#! for M&B v.903

from header_common import *
from header_operations import *
from header_mission_templates import *
from header_animations import *
from header_skills import *
from header_items import *
from module_constants import *
from header_colors import *

####################################################################################################################
#   Each mission-template is a tuple that contains the following fields:
#  1) Mission-template id (string): used for referencing mission-templates in other files.
#     The prefix mt_ is automatically added before each mission-template id
#
#  2) Mission-template flags (int): See header_mission-templates.py for a list of available flags
#  3) Mission-type(int): Which mission types this mission template matches.
#     For mission-types to be used with the default party-meeting system,
#     this should be 'charge' or 'charge_with_ally' otherwise must be -1.
#     
#  4) Mission description text (string).
#  5) List of spawn records (list): Each spawn record is a tuple that contains the following fields:
#    5.1) entry-no: Troops spawned from this spawn record will use this entry
#    5.2) spawn flags. 
#    5.3) alter flags. which equipment will be overriden
#    5.4) ai flags.
#    5.5) Number of troops to spawn.
#    5.6) list of equipment to add to troops spawned from here (maximum 8).
#  6) List of triggers (list).
#     See module_triggers.py for infomation about triggers.
#
#  Please note that mission templates is work in progress and can be changed in the future versions.
# 
####################################################################################################################

#pilgrim_disguise = [itm_pilgrim_hood,itm_pilgrim_disguise,itm_practice_staff, itm_throwing_daggers]
af_castle_lord = af_override_horse | af_override_weapons| af_require_civilian

mission_templates = [
(
    "town_default",0,-1,
    "Default town visit",
    [(0,mtef_scene_source|mtef_team_0,af_override_horse,0,1,[]),
     (1,mtef_scene_source|mtef_team_0,af_override_horse,0,1,[]),
	 (2,mtef_visitor_source,af_override_horse,0,1,[]),
	 (3,mtef_visitor_source,af_override_horse,0,1,[]),
	 (4,mtef_visitor_source,af_override_horse,0,1,[]),
	 (5,mtef_visitor_source,af_override_horse,0,1,[]),
	 (6,mtef_visitor_source,af_override_horse,0,1,[]),
	 (7,mtef_visitor_source,af_override_horse,0,1,[]),
     (8,mtef_visitor_source,af_override_horse,0,1,[]),(9,mtef_scene_source,af_override_horse,0,1,[]),(10,mtef_scene_source,af_override_horse,0,1,[]),(11,mtef_scene_source,af_override_horse,0,1,[]),
     (12,mtef_scene_source,af_override_horse,0,1,[]),(13,mtef_scene_source,0,0,1,[]),(14,mtef_scene_source,0,0,1,[]),(15,mtef_scene_source,0,0,1,[]),
     (16,mtef_visitor_source,af_override_horse,0,1,[]),(17,mtef_visitor_source,af_override_horse,0,1,[]),(18,mtef_visitor_source,af_override_horse,0,1,[]),(19,mtef_visitor_source,af_override_horse,0,1,[]),(20,mtef_visitor_source,af_override_horse,0,1,[]),(21,mtef_visitor_source,af_override_horse,0,1,[]),(22,mtef_visitor_source,af_override_horse,0,1,[]),(23,mtef_visitor_source,af_override_horse,0,1,[]),(24,mtef_visitor_source,af_override_horse,0,1,[]),
     (25,mtef_visitor_source,af_override_horse,0,1,[]),(26,mtef_visitor_source,af_override_horse,0,1,[]),(27,mtef_visitor_source,af_override_horse,0,1,[]),(28,mtef_visitor_source,af_override_horse,0,1,[]),(29,mtef_visitor_source,af_override_horse,0,1,[]),(30,mtef_visitor_source,af_override_horse,0,1,[]),(31,mtef_visitor_source,af_override_horse,0,1,[])
     ],
    [
      (ti_inventory_key_pressed, 0, 0, [(set_trigger_result,1)], []),
      (ti_tab_pressed, 0, 0, [(set_trigger_result,1)], []),
	  (ti_before_mission_start,0,0,[(eq,'$g_town_has_dojo',false)],
		[
			(set_fixed_point_multiplier,1),
			(scene_prop_get_num_instances,':no_spr_insts','spr_dojo'),
			(try_begin),
				(gt,':no_spr_insts',0),
				(scene_prop_get_instance,':spr_inst','spr_dojo',0),
				(prop_instance_get_position,pos1,':spr_inst'),
				(position_get_z,':pos_z',pos1),
				(val_sub,':pos_z',100),
				(position_set_z,pos1,':pos_z'),
				(prop_instance_set_position,':spr_inst',pos1),
				(prop_instance_animate_to_position,':spr_inst',pos1,1),
			(try_end),
			]),
    ],
  ),

# This template is used in party encounters and such.
# 
  (
    "conversation_encounter",0,-1,
    "Conversation_encounter",
    [( 0,mtef_visitor_source,af_override_fullhelm,0,1,[]),( 1,mtef_visitor_source,af_override_fullhelm,0,1,[]),
     ( 2,mtef_visitor_source,af_override_fullhelm,0,1,[]),( 3,mtef_visitor_source,af_override_fullhelm,0,1,[]),( 4,mtef_visitor_source,af_override_fullhelm,0,1,[]),( 5,mtef_visitor_source,af_override_fullhelm,0,1,[]),( 6,mtef_visitor_source,af_override_fullhelm,0,1,[]),
     ( 7,mtef_visitor_source,af_override_fullhelm,0,1,[]),( 8,mtef_visitor_source,af_override_fullhelm,0,1,[]),( 9,mtef_visitor_source,af_override_fullhelm,0,1,[]),(10,mtef_visitor_source,af_override_fullhelm,0,1,[]),(11,mtef_visitor_source,af_override_fullhelm,0,1,[]),
    #prisoners now...
     (12,mtef_visitor_source,af_override_fullhelm,0,1,[]),(13,mtef_visitor_source,af_override_fullhelm,0,1,[]),(14,mtef_visitor_source,af_override_fullhelm,0,1,[]),(15,mtef_visitor_source,af_override_fullhelm,0,1,[]),(16,mtef_visitor_source,af_override_fullhelm,0,1,[]),
    #Other party
     (17,mtef_visitor_source,af_override_fullhelm,0,1,[]),(18,mtef_visitor_source,af_override_fullhelm,0,1,[]),(19,mtef_visitor_source,af_override_fullhelm,0,1,[]),(20,mtef_visitor_source,af_override_fullhelm,0,1,[]),(21,mtef_visitor_source,af_override_fullhelm,0,1,[]),
     (22,mtef_visitor_source,af_override_fullhelm,0,1,[]),(23,mtef_visitor_source,af_override_fullhelm,0,1,[]),(24,mtef_visitor_source,af_override_fullhelm,0,1,[]),(25,mtef_visitor_source,af_override_fullhelm,0,1,[]),(26,mtef_visitor_source,af_override_fullhelm,0,1,[]),
     (27,mtef_visitor_source,af_override_fullhelm,0,1,[]),(28,mtef_visitor_source,af_override_fullhelm,0,1,[]),(29,mtef_visitor_source,af_override_fullhelm,0,1,[]),(30,mtef_visitor_source,af_override_fullhelm,0,1,[]),(31,mtef_visitor_source,af_override_fullhelm,0,1,[]),
     ],
    [],
  ),
  
#----------------------------------------------------------------
#mission templates before this point are hardwired into the game.
#-----------------------------------------------------------------
(
    "visit_town_horseback",0,-1,
    "Default town visit",
    [
	 # player eps
	 (0,mtef_scene_source|mtef_team_0,0,0,1,[]),
     (1,mtef_scene_source|mtef_team_0,0,0,1,[]),
	 (2,mtef_scene_source|mtef_team_0,0,0,1,[]),
	 (3,mtef_scene_source|mtef_team_0,0,0,1,[]),
	 (4,mtef_scene_source|mtef_team_0,0,0,1,[]),
	 # guard eps, up to 8 per scene
	 (5,mtef_visitor_source,af_override_horse,0,1,[]),
	 (6,mtef_visitor_source,af_override_horse,0,1,[]),
	 (7,mtef_visitor_source,af_override_horse,0,1,[]),
     (8,mtef_visitor_source,af_override_horse,0,1,[]),
	 (9,mtef_visitor_source,af_override_horse,0,1,[]),
	 (10,mtef_visitor_source,af_override_horse,0,1,[]),
	 (11,mtef_visitor_source,af_override_horse,0,1,[]),
     (12,mtef_visitor_source,af_override_horse,0,1,[]),
	 (13,mtef_visitor_source,0,0,1,[]),
	 (14,mtef_visitor_source,0,0,1,[]),
	 (15,mtef_visitor_source,0,0,1,[]),
     (16,mtef_visitor_source,af_override_horse,0,1,[]),(17,mtef_visitor_source,af_override_horse,0,1,[]),(18,mtef_visitor_source,af_override_horse,0,1,[]),(19,mtef_visitor_source,af_override_horse,0,1,[]),(20,mtef_visitor_source,af_override_horse,0,1,[]),(21,mtef_visitor_source,af_override_horse,0,1,[]),(22,mtef_visitor_source,af_override_horse,0,1,[]),(23,mtef_visitor_source,af_override_horse,0,1,[]),
     (24,mtef_visitor_source,af_override_horse,0,1,[]),(25,mtef_visitor_source,af_override_horse,0,1,[]),(26,mtef_visitor_source,af_override_horse,0,1,[]),(27,mtef_visitor_source,af_override_horse,0,1,[]),(28,mtef_visitor_source,af_override_horse,0,1,[]),(29,mtef_visitor_source,af_override_horse,0,1,[]),(30,mtef_visitor_source,af_override_horse,0,1,[]),(31,mtef_visitor_source,af_override_horse,0,1,[]),
     (32,mtef_visitor_source,af_override_horse,0,1,[]),(33,mtef_visitor_source,af_override_horse,0,1,[]),(34,mtef_visitor_source,af_override_horse,0,1,[]),(35,mtef_visitor_source,af_override_horse,0,1,[]),(36,mtef_visitor_source,af_override_horse,0,1,[]),(37,mtef_visitor_source,af_override_horse,0,1,[]),(38,mtef_visitor_source,af_override_horse,0,1,[]),(39,mtef_visitor_source,af_override_horse,0,1,[]),
     (40,mtef_visitor_source,af_override_horse,0,1,[]),(41,mtef_visitor_source,af_override_horse,0,1,[]),(42,mtef_visitor_source,af_override_horse,0,1,[]),(43,mtef_visitor_source,af_override_horse,0,1,[]),(44,mtef_visitor_source,af_override_horse,0,1,[]),(45,mtef_visitor_source,af_override_horse,0,1,[]),(46,mtef_visitor_source,af_override_horse,0,1,[]),(47,mtef_visitor_source,af_override_horse,0,1,[]),
     ],
    [
		(1,0,ti_once,[],[(call_script,'script_init_town_walker_agents')]),
		(3,0,0,[],[(call_script,'script_tick_town_walkers')]),
      (ti_inventory_key_pressed, 0, 0, [(set_trigger_result,1)], []),
      (ti_tab_pressed, 0, 0, [(set_trigger_result,1)], []),
	  (ti_before_mission_start,0,0,[(eq,'$g_town_has_dojo',false)],
		[
			(set_fixed_point_multiplier,1),
			(scene_prop_get_num_instances,':no_spr_insts','spr_dojo'),
			(try_begin),
				(gt,':no_spr_insts',0),
				(scene_prop_get_instance,':spr_inst','spr_dojo',0),
				(prop_instance_get_position,pos1,':spr_inst'),
				(position_get_z,':pos_z',pos1),
				(val_sub,':pos_z',100),
				(position_set_z,pos1,':pos_z'),
				(prop_instance_set_position,':spr_inst',pos1),
				(prop_instance_animate_to_position,':spr_inst',pos1,1),
			(try_end),
			]),
    ],
  ),
  (
    "town_center",0,-1,
    "Default town visit",
    [(0,mtef_scene_source|mtef_team_0,af_override_horse,0,1,[]),
     (1,mtef_scene_source|mtef_team_0,0,0,1,[]),
     (2,mtef_scene_source|mtef_team_0,af_override_horse,0,1,[]),
     (3,mtef_scene_source|mtef_team_0,af_override_horse,0,1,[]),
     (4,mtef_scene_source|mtef_team_0,af_override_horse,0,1,[]),
     (5,mtef_scene_source|mtef_team_0,af_override_horse,0,1,[]),
     (6,mtef_scene_source|mtef_team_0,af_override_horse,0,1,[]),
     (7,mtef_scene_source|mtef_team_0,af_override_horse,0,1,[]),
     
     (8,mtef_scene_source,af_override_horse,0,1,[]),
     (9,mtef_scene_source,af_override_horse,0,1,[]),(10,mtef_scene_source,af_override_horse,0,1,[]),(11,mtef_scene_source,af_override_horse,0,1,[]),(12,mtef_scene_source,af_override_horse,0,1,[]),(13,mtef_scene_source,0,0,1,[]),(14,mtef_scene_source,0,0,1,[]),(15,mtef_scene_source,0,0,1,[]),
     (16,mtef_visitor_source,af_override_horse,0,1,[]),(17,mtef_visitor_source,af_override_horse,0,1,[]),(18,mtef_visitor_source,af_override_horse,0,1,[]),(19,mtef_visitor_source,af_override_horse,0,1,[]),(20,mtef_visitor_source,af_override_horse,0,1,[]),(21,mtef_visitor_source,af_override_horse,0,1,[]),(22,mtef_visitor_source,af_override_horse,0,1,[]),(23,mtef_visitor_source,af_override_horse,0,1,[]),
     (24,mtef_visitor_source,af_override_horse,0,1,[]),(25,mtef_visitor_source,af_override_horse,0,1,[]),(26,mtef_visitor_source,af_override_horse,0,1,[]),(27,mtef_visitor_source,af_override_horse,0,1,[]),(28,mtef_visitor_source,af_override_horse,0,1,[]),(29,mtef_visitor_source,af_override_horse,0,1,[]),(30,mtef_visitor_source,af_override_horse,0,1,[]),(31,mtef_visitor_source,af_override_horse,0,1,[]),
     (32,mtef_visitor_source,af_override_horse,0,1,[]),(33,mtef_visitor_source,af_override_horse,0,1,[]),(34,mtef_visitor_source,af_override_horse,0,1,[]),(35,mtef_visitor_source,af_override_horse,0,1,[]),(36,mtef_visitor_source,af_override_horse,0,1,[]),(37,mtef_visitor_source,af_override_horse,0,1,[]),(38,mtef_visitor_source,af_override_horse,0,1,[]),(39,mtef_visitor_source,af_override_horse,0,1,[]),
     (40,mtef_visitor_source,af_override_horse,0,1,[]),(41,mtef_visitor_source,af_override_horse,0,1,[]),(42,mtef_visitor_source,af_override_horse,0,1,[]),(43,mtef_visitor_source,af_override_horse,0,1,[]),(44,mtef_visitor_source,af_override_horse,0,1,[]),(45,mtef_visitor_source,af_override_horse,0,1,[]),(46,mtef_visitor_source,af_override_horse,0,1,[]),(47,mtef_visitor_source,af_override_horse,0,1,[]),
     ],
    [
      (ti_inventory_key_pressed, 0, 0, [(set_trigger_result,1)], []),
      (ti_tab_pressed, 0, 0, [(set_trigger_result,1)], []),
	  (ti_before_mission_start,0,0,[(eq,'$g_town_has_dojo',true)],
		[
			(set_fixed_point_multiplier,1),
			(scene_prop_get_num_instances,':no_spr_insts','spr_dojo'),
			(try_begin),
				(gt,':no_spr_insts',0),
				(scene_prop_get_instance,':spr_inst','spr_dojo',0),
				(prop_instance_get_position,pos1,':spr_inst'),
				(position_get_z,':pos_z',pos1),
				(val_sub,':pos_z',100),
				(position_set_z,pos1,':pos_z'),
				(prop_instance_set_position,':spr_inst',pos1),
				(prop_instance_animate_to_position,':spr_inst',pos1,1),
			(try_end),
			]),
    ],
  ),


##### TODO: QUESTS COMMENT OUT BEGIN
##  (
##    "back_alley_battle",mtf_arena_fight,-1,
##    "You enter the back alley",
##    [
##      (0,mtef_visitor_source|mtef_team_0,af_override_horse,aif_start_alarmed,1,[]),
##      (1,mtef_visitor_source|mtef_team_1,af_override_horse,aif_start_alarmed,1,[]),
##    ],
##    [
##      (ti_inventory_key_pressed, 0, 0, [(display_message,"str_cant_use_inventory_now")], []),
##      (ti_tab_pressed, 0, 0, [(display_message,"str_cant_leave_now")], []),
##      (ti_before_mission_start, 0, 0, [], [(call_script, "script_change_banners")]),
##
##      (0, 0, ti_once, [
##          (store_mission_timer_a,":cur_time"),
##          (ge,":cur_time",1), 
##          (assign, ":merchant_hp", 0),
##          (assign, ":player_hp", 0),
##          (assign, ":merchant_hp", 0),
##          (assign, ":merchant_agent", -1),
##          (assign, ":player_agent", -1),
##          (try_for_agents, ":agent_no"),
##            (agent_get_troop_id, ":troop_id", ":agent_no"),
##            (try_begin),
##              (eq, ":troop_id", "trp_local_merchant"),
##              (store_agent_hit_points, ":merchant_hp",":agent_no"),
##              (assign, ":merchant_agent", ":agent_no"),
##            (else_try),
##              (eq, ":troop_id", "trp_player"),
##              (store_agent_hit_points, ":player_hp",":agent_no"),
##              (assign, ":player_agent", ":agent_no"),
##            (try_end),
##          (try_end),
##          (agent_is_alive, ":player_agent"),
##          (agent_is_alive, ":merchant_agent"),
##          (is_between, ":merchant_hp", 1, 30),
##          (gt, ":player_hp", 50),
##          (start_mission_conversation, "trp_local_merchant"),
##          ], []),
##      
##      (1, 4, ti_once, [(this_or_next|main_hero_fallen),(num_active_teams_le,1)],
##       [
##           (try_begin),
##             (main_hero_fallen),
##             (fail_quest, "qst_kill_local_merchant"),
##           (else_try),
##             (succeed_quest, "qst_kill_local_merchant"),
##           (try_end),
##           (try_begin),
##             (eq, "$sneaked_into_town",1),
##             (assign,":cur_mission_template","mt_town_disguised"),
##           (else_try),
##             (assign,":cur_mission_template","mt_town_default"),
##           (try_end),
##           (set_jump_mission,":cur_mission_template"),
##           (party_get_slot, ":cur_town_center", "$current_town", slot_town_center),
##           (set_jump_entry, 0),
##           (jump_to_scene,":cur_town_center"),
##           ]),
##    ],
##  ),
##
##  (
##    "arena_duel",mtf_arena_fight|mtf_commit_casualties,-1,
##    "You enter the arena",
##    [
##      (0,mtef_visitor_source|mtef_team_0,af_override_horse,aif_start_alarmed,1,[]),
##      (1,mtef_visitor_source|mtef_team_0,af_override_horse,aif_start_alarmed,1,[itm_practice_sword,itm_practice_shield]),
##      (2,mtef_visitor_source|mtef_team_0,af_override_horse,aif_start_alarmed,1,[]),
##      (3,mtef_visitor_source|mtef_team_0,af_override_horse,aif_start_alarmed,1,[]),
##      (4,mtef_visitor_source|mtef_team_0,af_override_horse,aif_start_alarmed,1,[itm_practice_sword,itm_practice_shield]),
##      (5,mtef_visitor_source|mtef_team_0,af_override_horse,aif_start_alarmed,1,[itm_practice_sword,itm_practice_shield]),
##      (6,mtef_visitor_source|mtef_team_0,af_override_horse,aif_start_alarmed,1,[itm_practice_sword,itm_practice_shield]),
##      (7,mtef_visitor_source|mtef_team_0,af_override_horse,aif_start_alarmed,1,[itm_practice_sword,itm_practice_shield]),
##      (8,mtef_visitor_source|mtef_team_1,af_override_horse|af_override_weapons,aif_start_alarmed,1,[itm_practice_sword,itm_practice_shield]),
##    ],
##    [
##      (ti_inventory_key_pressed, 0, 0, [(display_message,"str_cant_use_inventory_now")], []),
##      (ti_tab_pressed, 0, 0, [(display_message,"str_cant_leave_now")], []),
##      (ti_before_mission_start, 0, 0, [], [(call_script, "script_change_banners")]),
##
##      (1, 4, ti_once, [(this_or_next|main_hero_fallen),(num_active_teams_le,1)],
##       [
##           (try_begin),
##             (main_hero_fallen),
##             (fail_quest, "qst_duel_for_lady"),
##           (else_try),
##             (succeed_quest, "qst_duel_for_lady"),
##           (try_end),
##           (finish_mission),
##           ]),
##    ],
##  ),
##### TODO: QUESTS COMMENT OUT END

  (
    "lead_charge",mtf_battle_mode,charge,
    "You lead your men to battle.",
    [
     (1,mtef_defenders|mtef_team_0,0,aif_start_alarmed,20,[]),
     (0,mtef_defenders|mtef_team_0,0,aif_start_alarmed,0,[]),
     (4,mtef_attackers|mtef_team_1,0,aif_start_alarmed,20,[]),
     (4,mtef_attackers|mtef_team_1,0,aif_start_alarmed,0,[]),
     ],
    [
      (ti_tab_pressed, 0, 0, [],
       [
           (try_begin),
             (eq, "$battle_won", 1),
             (call_script, "script_count_mission_casualties_from_agents"),
             (finish_mission,0),
           (else_try),
             (call_script, "script_cf_check_enemies_nearby"),
             (question_box,"str_do_you_want_to_retreat",'str_yes','str_no'),
           (else_try),
             (display_message,"str_can_not_retreat"),
           (try_end),
        ]),
      (ti_question_answered, 0, 0, [],
       [(store_trigger_param_1,":answer"),
        (eq,":answer",0),
        (assign, "$pin_player_fallen", 0),
        (call_script, "script_simulate_retreat", 10, 30),
        (call_script, "script_count_mission_casualties_from_agents"),
        (finish_mission,0),]),

      (ti_before_mission_start, 0, 0, [],
       [
           (team_set_relation, 0, 2, 1),
        ]),
      
      (0, 0, ti_once, [], [(assign,"$battle_won",0),
                           (assign,"$defender_reinforcement_stage",0),
                           (assign,"$attacker_reinforcement_stage",0),
                           (assign, "$g_presentation_battle_active", 0),
                           ]),

##      (ti_on_agent_spawn, 0, 0, [],
##       [(store_trigger_param_1, ":agent_no"),
##        (call_script, "script_agent_reassign_team", ":agent_no"),
##        ]),


      (2, 0, 0, [], [(call_script, "script_check_friendly_kills"),
                     ]),

      (1, 0, 5,
		[
			(lt,"$defender_reinforcement_stage",12),
			(store_mission_timer_a,":mission_time"),
			(ge,":mission_time",10),
			(store_normalized_team_count,":num_defenders", 0),
			(lt,":num_defenders",12),
			],
		[
			(add_reinforcements_to_entry,0,12),
			(val_add,"$defender_reinforcement_stage",1),
			(try_for_agents,':agent_no'),
				(call_script,'script_cf_process_team_reinforcements',':agent_no'),
			(try_end),
			(call_script,'script_update_team_morale'),
			]
		),
      
      (1, 0, 5,
		[
			(lt,"$attacker_reinforcement_stage",12),
			(store_mission_timer_a,":mission_time"),
			(ge,":mission_time",10),
			(store_normalized_team_count,":num_attackers", 1),
			(lt,":num_attackers",12),
			],
		[
			(add_reinforcements_to_entry,3,12),
			(val_add,"$attacker_reinforcement_stage",1),
			(try_for_agents,':agent_no'),
				(call_script,'script_cf_process_team_reinforcements',':agent_no'),
			(try_end),
			(call_script,'script_update_team_morale'),
			]
		),
      
(1, 60, ti_once,
	[
		(store_mission_timer_a,reg1),
		(ge,reg1,10),
		(all_enemies_defeated,2),
		(neg|main_hero_fallen,0),
		(set_mission_result,1),
		(call_script, "script_count_mission_casualties_from_agents"),
		(assign,':num_remaining_horses',0),
		(try_for_range,':agent_no'),
			(neg|agent_is_human,':agent_no'),
			(agent_is_alive,':agent_no'),
			(val_add,':num_remaining_horses',1),
		(try_end),
		(val_add,'$g_mission_remaining_horses',':num_remaining_horses'),
		(party_get_num_companions,":opp_size","$g_encountered_party"),
		(store_faction_of_party,":opp_fac","$g_encountered_party"),
		(party_get_num_companions,":plyr_size","p_main_party"),
		(call_script,'script_post_battle_cleanup'),
		(call_script,"script_assign_fame",':opp_size',':opp_fac',':plyr_size'),
		(call_script,"script_award_xp",':opp_size'),
		(call_script,"script_assign_fp",':opp_size',':opp_fac'),
		(assign, "$g_battle_result", 1),
		(display_message,"str_msg_battle_won"),
		(assign,"$battle_won",1),
		],
	[
		(finish_mission,1),
		]
	),
      (10, 0, 0, [], [(eq,"$battle_won",1),(display_message,"str_msg_battle_won")]),
      (1, 4, ti_once, [(main_hero_fallen)],
          [
              (assign, "$pin_player_fallen", 1),
              (call_script, "script_simulate_retreat", 20, 30),
              (assign, "$g_battle_result", -1),
              (set_mission_result,-1),
              (call_script, "script_count_mission_casualties_from_agents"),
              (finish_mission,0)]),
      (ti_inventory_key_pressed, 0, 0, [(display_message,"str_use_baggage_for_inventory")], []),


      #AI Triggers
      (0, 2, ti_once, [
          (store_mission_timer_a,":mission_time"),(ge,":mission_time",1),
          ],
       [(call_script, "script_select_battle_tactic"),
        (call_script, "script_battle_tactic_init")]),
      
      (1, 0, 0, [
          (store_mission_timer_a,":mission_time"),(ge,":mission_time",2),
          (call_script, "script_battle_tactic_apply"),
          ], []),

     (0, 0, 0,
      [(key_clicked, key_backspace),
       (start_presentation, "prsnt_battle"),
        ], []),

     (0.1, 0, 0,
      [(eq, "$g_presentation_battle_active", 1),
       (call_script, "script_update_order_panel_statistics_and_map"),
        ], []),
(ti_before_mission_start,0,0.0,[],
	[
		(assign,"$player_mission_kills",0),
		]
	),

(ti_once, 0, 0, [],
	[
		(get_player_agent_no,':player_agent_no'),
		(store_agent_hit_points,'$player_mission_health',':player_agent_no'),
		]
	),

# weapons and armor durability code
(0.2, 0, 0, [],
	[
		(get_player_agent_kill_count,":player_kills",0),
		(assign, ":past_kills", "$player_mission_kills"),  #player_mission_kills is set to 0 whenever a mission starts.
		(assign, "$player_mission_kills", ":player_kills"),
		(get_player_agent_no,":player_agent_no"),
		(try_begin),
			(gt, ":player_kills", ":past_kills"),
			(agent_get_wielded_item,":wielded_item",":player_agent_no",0),
			(item_get_slot,':weapon_type',':wielded_item',slot_item_type),
			(try_begin),
				(eq,':weapon_type',sit_metal_weapon),
				(store_random_in_range,':weapon_damage',1,6),
				(val_add,':weapon_damage',3),
				(call_script,'script_modify_item_durability',':wielded_item',':weapon_damage'),
			(else_try),
				(eq,':weapon_type',sit_wooden_weapon),
				(store_random_in_range,':weapon_damage',0,5),
				(val_add,':weapon_damage',1),
				(call_script,'script_modify_item_durability',':wielded_item',':weapon_damage'),
			(else_try),
				(eq,':weapon_type',sit_metal_weapon),
				(store_random_in_range,':weapon_damage',1,7),
				(val_add,':weapon_damage',3),
				(call_script,'script_modify_item_durability',':wielded_item',':weapon_damage'),
			(else_try),
				(eq,':weapon_type',sit_ranged_weapon),
				(store_random_in_range,':weapon_damage',0,4),
				(val_add,':weapon_damage',2),
				(call_script,'script_modify_item_durability',':wielded_item',':weapon_damage'),
			(try_end),
		(try_end),
		(store_agent_hit_points,':current_health',':player_agent_no'),
		(try_begin),
			(lt,':current_health','$player_mission_health'),
			(assign,'$player_mission_health',':current_health'),
			# armor
			(try_begin),
				(troop_get_inventory_slot,':player_armor','trp_player',ek_body),
				(gt,':player_armor',0),
				(store_random_in_range,':armor_damage',0,6),
				(val_add,':armor_damage',3),
				(call_script,'script_modify_item_durability',':player_armor',':weapon_damage'),
			(try_end),
			# helmet
			(try_begin),
				(troop_get_inventory_slot,':player_helmet','trp_player',ek_head),
				(gt,':player_helmet',0),
				(store_random_in_range,':armor_damage',0,4),
				(val_add,':armor_damage',2),
				(call_script,'script_modify_item_durability',':player_helmet',':weapon_damage'),
			(try_end),
			# boots
			(try_begin),
				(troop_get_inventory_slot,':player_boots','trp_player',ek_foot),
				(gt,':player_boots',0),
				(store_random_in_range,':armor_damage',0,6),
				(val_add,':armor_damage',1),
				(call_script,'script_modify_item_durability',':player_boots',':weapon_damage'),
			(try_end),
			# gloves
			(try_begin),
				(troop_get_inventory_slot,':player_gloves','trp_player',ek_gloves),
				(gt,':player_gloves',0),
				(store_random_in_range,':armor_damage',0,3),
				(call_script,'script_modify_item_durability',':player_gloves',':weapon_damage'),
			(try_end),
		(try_end),
		]
	),
# archers find high ground command
# (0.0, 0, 0.0, [(key_clicked,key_h)],
	# [
		# (assign,':end_loop',false),
		# (get_player_agent_no,':player_agent_no'),
		# (agent_get_team,':player_team',':player_agent_no'),
		# (agent_get_position,pos1,':player_agent_no'),
		# (set_fixed_point_multiplier,100),
		# (get_scene_boundaries,pos60,pos61),
		# (position_get_x,':min_x',pos60),
		# (position_get_y,':min_y',pos60),
		# (position_get_x,':max_x',pos61),
		# (position_get_y,':max_y',pos61),
		# (try_for_range,':unused',0,1000),
			# (eq,':end_loop',false),
			# set positions to pos1
			# (copy_position,pos2,pos1),
			# (copy_position,pos3,pos1),
			# (copy_position,pos4,pos1),
			# (copy_position,pos5,pos1),
			# (call_script,'script_position_add_y',pos2,100,true,':min_y',':max_y'),
			# (call_script,'script_position_add_x',pos3,100,true,':min_x',':max_x'),
			# (call_script,'script_position_add_y',pos4,-100,true,':min_y',':max_y'),
			# (call_script,'script_position_add_x',pos5,-100,true,':min_x',':max_x'),
			# set all positions to ground level and get z values
			# (position_set_z_to_ground_level,pos2),
			# (position_set_z_to_ground_level,pos3),
			# (position_set_z_to_ground_level,pos4),
			# (position_set_z_to_ground_level,pos5),
			# (position_get_z,':pos1_z',pos1),
			# (position_get_z,':pos2_z',pos2),
			# (position_get_z,':pos3_z',pos3),
			# (position_get_z,':pos4_z',pos4),
			# (position_get_z,':pos5_z',pos5),
			# compare z values
			# (store_sub,':dz_21',':pos2_z',':pos1_z'),
			# (store_sub,':dz_31',':pos3_z',':pos1_z'),
			# (store_sub,':dz_41',':pos4_z',':pos1_z'),
			# (store_sub,':dz_51',':pos5_z',':pos1_z'),
			# (try_begin),
				# all values are negative, pos1 at a local maximum
				# (le,':dz_21',0),
				# (le,':dz_31',0),
				# (le,':dz_41',0),
				# (le,':dz_51',0),
				# (assign,':end_loop',true),
			# (else_try),
				# (assign,':dz_21_temp',':dz_21'),
				# (assign,':dz_31_temp',':dz_31'),
				# (assign,':dz_41_temp',':dz_41'),
				# (assign,':dz_51_temp',':dz_51'),
				# (val_max,':dz_21_temp',':dz_31_temp'),
				# (val_max,':dz_21_temp',':dz_41_temp'),
				# (val_max,':dz_21_temp',':dz_41_temp'),
				# (try_begin),
					# (eq,':dz_21_temp',':dz_21'),
					# (copy_position,pos1,pos2),
				# (else_try),
					# (eq,':dz_31_temp',':dz_31'),
					# (copy_position,pos1,pos3),
				# (else_try),
					# (eq,':dz_41_temp',':dz_41'),
					# (copy_position,pos1,pos4),
				# (else_try),
					# (eq,':dz_51_temp',':dz_51'),
					# (copy_position,pos1,pos5),
				# (try_end),
			# (try_end),
		# (try_end),
		# (team_set_order_position,':player_team',grc_archers,pos1),
		# (team_give_order,':player_team',grc_archers,mordr_hold),
		# (display_message,'@Archers, take high ground!',purple_text),
		# ]
	# ),
# agent morale triggers
# initialize morale
# notes: pos0-pos3 are reserved
# pos0 is player team start position, pos1 is enemy team start position
# pos2 is (0,0) on the scene, pos3 is (maxX,maxY) on the scene)
(0,0,ti_once,[],
	[
		(assign,'$g_player_team_morale',100),
		(assign,'$g_enemy_team_morale',100),
		(assign,'$g_enemy_size',0),
		(assign,'$g_ally_size',0),
		(assign,'$g_player_rally_command_used',false),
		(call_script,'script_clear_temp_array','trp_temp_array_a',250),
		(call_script,'script_clear_temp_array','trp_temp_array_b',250),
		(call_script,'script_clear_temp_array','trp_temp_array_c',250),
		(call_script,'script_clear_temp_array','trp_temp_array_d',250),
		(get_player_agent_no,':player_agent_no'),
		(agent_get_position,pos0,':player_agent_no'),
		(agent_get_team,'$g_player_team',':player_agent_no'),
		(team_set_leader,'$g_player_team',':player_agent_no'),
		(assign,':temp_array_index_a',0),
		(assign,':temp_array_index_c',0),
		(try_for_agents,':agent_no'),
			(agent_is_human,':agent_no'),
			(agent_is_alive,':agent_no'),
			(agent_get_team,':agent_team',':agent_no'),
			(teams_are_enemies,':agent_team','$g_player_team'),
			(assign,'$g_enemy_team',':agent_team'),
			(agent_get_troop_id,':agent_troop_id',':agent_no'),
			(try_begin),
				(troop_is_hero,':agent_troop_id'),
				(troop_set_slot,'trp_temp_array_a',':temp_array_index_a',':agent_troop_id'),
				(troop_set_slot,'trp_temp_array_b',':temp_array_index_a',':agent_no'),
				(val_add,':temp_array_index_a',1),
			(else_try),
				(troop_set_slot,'trp_temp_array_c',':temp_array_index_c',':agent_troop_id'),
				(troop_set_slot,'trp_temp_array_d',':temp_array_index_c',':agent_no'),
				(val_add,':temp_array_index_c',1),
			(try_end),
		(try_end),
		# assign enemy team leader
		(assign,':skill_level',-1),
		(assign,':temp_array_length_a',':temp_array_index_a'),
		(assign,':temp_array_length_c',':temp_array_index_c'),
		(try_for_range,':temp_array_index',0,':temp_array_length_a'),
			(troop_get_slot,':agent_troop_id','trp_temp_array_a',':temp_array_index'),
			(store_skill_level,':leadership_skill',skl_leadership,':agent_troop_id'),
			(gt,':leadership_skill',':skill_level'),
			(assign,':skill_level',':leadership_skill'),
			(troop_get_slot,':agent_no','trp_temp_array_b',':temp_array_index'),
			(team_set_leader,'$g_enemy_team',':agent_no'),
		(try_end),
		(try_begin),
			(eq,':skill_level',-1),
			(try_for_range,':temp_array_index',0,':temp_array_length_c'),
				(troop_get_slot,':agent_troop_id','trp_temp_array_c',':temp_array_index'),
				(store_character_level,':leadership_skill',':agent_troop_id'),
				(gt,':leadership_skill',':skill_level'),
				(assign,':skill_level',':leadership_skill'),
				(troop_get_slot,':agent_no','trp_temp_array_d',':temp_array_index'),
				(team_set_leader,'$g_enemy_team',':agent_no'),
			(try_end),
		(try_end),
		(team_get_leader,':enemy_leader','$g_enemy_team'),
		(agent_get_position,pos1,':enemy_leader'),
		(try_for_agents,':agent_no'),
			(agent_is_human,':agent_no'),
			(agent_is_alive,':agent_no'),
			(agent_get_party_id,':agent_party',':agent_no'),
			(try_begin),
				(this_or_next|eq,':agent_party','p_main_party'),
				(agent_is_ally,':agent_no'),
				(agent_set_team,':agent_no','$g_player_team'),
				(store_agent_hit_points,':agent_hp',':agent_no'),
				(call_script,'script_object_modify_slot',obj_agent,':agent_no',slot_agent_morale,op_asn,':agent_hp'),
				# (call_script,'script_agent_get_level',':agent_no'),
				# (assign,':agent_level',reg0),
				# (store_mul,':morale',':agent_hp',':agent_level'),
				(val_add,'$g_ally_size',1),
			(else_try),
				(store_agent_hit_points,':agent_hp',':agent_no'),
				(agent_set_team,':agent_no','$g_enemy_team'),
				(call_script,'script_object_modify_slot',obj_agent,':agent_no',slot_agent_morale,op_asn,':agent_hp'),
				# (call_script,'script_agent_get_level',':agent_no'),
				# (assign,':agent_level',reg0),
				# (store_mul,':morale',':agent_hp',':agent_level'),
				(val_add,'$g_enemy_size',1),
			(try_end),
		(try_end),
		(assign,reg10,'$g_ally_size'),
		(assign,reg11,'$g_enemy_size'),
		(display_message,'@{reg10} allies, {reg11} enemies',lt_blue_text),
		(display_message,"@Hit 'V' to rally your men if they have been routed (one time only)",lt_blue_text),
		]
	),
# update morale
(1.0, 0, 0.0, [],
	[
		(call_script,'script_update_team_morale'),
		]
	),
# morale check
(8.0, 0, 0.0, [],
	[
		(get_scene_boundaries,pos2,pos3),
		(try_begin),
			(lt,'$g_player_team_morale',80),
			(try_for_agents,':agent_no'),
				(agent_is_human,':agent_no'),
				(agent_is_alive,':agent_no'),
				(agent_get_team,':agent_team',':agent_no'),
				(eq,':agent_team','$g_player_team'),
				(call_script,'script_cf_agent_check_morale','$g_player_team_morale'),
				(get_distance_between_positions,':dist1',pos0,pos2),
				(get_distance_between_positions,':dist2',pos0,pos3),
				(try_begin),
					(gt,':dist1',':dist2'),
					(agent_set_scripted_destination,':agent_no',pos3,1),
				(else_try),
					(agent_set_scripted_destination,':agent_no',pos2,1),
				(try_end),
			(try_end),
		(else_try),
			(lt,'$g_enemy_team_morale',80),
			(try_for_agents,':agent_no'),
				(agent_is_human,':agent_no'),
				(agent_is_alive,':agent_no'),
				(agent_get_team,':agent_team',':agent_no'),
				(eq,':agent_team','$g_enemy_team'),
				(call_script,'script_cf_agent_check_morale','$g_enemy_team_morale'),
				(get_distance_between_positions,':dist1',pos1,pos2),
				(get_distance_between_positions,':dist2',pos1,pos3),
				(try_begin),
					(gt,':dist1',':dist2'),
					(agent_set_scripted_destination,':agent_no',pos3,1),
				(else_try),
					(agent_set_scripted_destination,':agent_no',pos2,1),
				(try_end),
			(try_end),
		(try_end),
		(assign,reg10,'$g_player_team_morale'),
		(assign,reg11,'$g_enemy_team_morale'),
		(str_store_string,s10,'@Player team'),
		(str_store_string,s11,'@Enemy team'),
		(display_message,'@{s10} morale: {reg10}; {s11} morale: {reg11}',debug_text),
		]
	),
# (7.5, 0, 0.0, [],
	# [
		# (team_get_leader,':player_team_leader','$g_player_team'),
		# (team_get_leader,':enemy_team_leader','$g_enemy_team'),
		# (agent_get_troop_id,':player_team_leader_troop_id',':player_team_leader'),
		# (agent_get_troop_id,':enemy_team_leader_troop_id',':enemy_team_leader'),
		# (store_skill_level,':player_team_leadership',skl_leadership,':player_team_leader_troop_id'),
		# (store_skill_level,':enemy_team_leadership',skl_leadership,':enemy_team_leader_troop_id'),
		# (agent_get_position,pos10,':player_team_leader'),
		# (agent_get_position,pos11,':enemy_team_leader'),
		# (try_for_agents,':agent_no'),
			# (agent_is_human,':agent_no'),
			# (agent_is_alive,':agent_no'),
			# (agent_get_team,':agent_team',':agent_no'),
			# (agent_get_position,pos12,':agent_no'),
			# (try_begin),
				# (eq,':agent_team','$g_player_team'),
				# (get_distance_between_positions,':dist',pos10,pos12),
				# (try_begin),
					# (is_between,':dist',0,1000),
					# (assign,':multiplier',4),
				# (else_try),
					# (is_between,':dist',1000,5000),
					# (assign,':multiplier',2),
				# (else_try),
					# (assign,':multiplier',1),
				# (try_end),
				# (store_mul,':morale_bonus',':player_team_leadership',':multiplier'),
			# (else_try),
				# (eq,':agent_team','$g_enemy_team'),
				# (get_distance_between_positions,':dist',pos11,pos12),
				# (try_begin),
					# (is_between,':dist',0,1000),
					# (assign,':multiplier',4),
				# (else_try),
					# (is_between,':dist',1000,5000),
					# (assign,':multiplier',2),
				# (else_try),
					# (assign,':multiplier',1),
				# (try_end),
				# (store_mul,':morale_bonus',':enemy_team_leadership',':multiplier'),
			# (try_end),
			# (call_script,'script_object_modify_slot',obj_agent,':agent_no',slot_agent_morale,op_add,':morale_bonus'),
		# (try_end),
		# ]
	# ),
# routing check
(4.0, 0, 0.0, [],
	[
		(store_sub,':morale_diff','$g_player_team_morale','$g_enemy_team_morale'),
		(get_scene_boundaries,pos2,pos3),
		(try_begin),
			(le,':morale_diff',-40),
			(display_message,'@Your men have been routed!',red_text),
			(try_for_agents,':agent_no'),
				(agent_is_human,':agent_no'),
				(agent_is_alive,':agent_no'),
				(agent_get_team,':agent_team',':agent_no'),
				(eq,':agent_team','$g_player_team'),
				(call_script,'script_cf_agent_check_rout',':agent_no'),
				(get_scene_boundaries,pos2,pos3),
				(position_get_x,':pos0x',pos0),
				(position_get_y,':pos0y',pos0),
				(position_get_x,':pos1x',pos1),
				(position_get_y,':pos1y',pos1),
				(position_get_x,':pos2x',pos2),
				(position_get_y,':pos2y',pos2),
				(position_get_x,':pos3x',pos3),
				(position_get_y,':pos3y',pos3),
				(call_script,'script_agent_get_valid_retreat_position',0,':pos0x',':pos0y',':pos1x',':pos1y',':pos2x',':pos2y',':pos3x',':pos3y'),
				(init_position,pos5),
				(assign,':pos5x',reg0),
				(assign,':pos5y',reg1),
				(position_set_x,pos5,':pos5x'),
				(position_set_y,pos5,':pos5y'),
				(position_set_z_to_ground_level,pos5),
				(agent_set_scripted_destination,':agent_no',pos5,1),
			(try_end),
		(else_try),
			(ge,':morale_diff',40),
			(display_message,'@Your enemy has been routed!',lt_red_text),
			(try_for_agents,':agent_no'),
				(agent_is_human,':agent_no'),
				(agent_is_alive,':agent_no'),
				(agent_get_team,':agent_team',':agent_no'),
				(eq,':agent_team','$g_enemy_team'),
				(call_script,'script_cf_agent_check_rout',':agent_no'),
				(get_scene_boundaries,pos2,pos3),
				(position_get_x,':pos0x',pos0),
				(position_get_y,':pos0y',pos0),
				(position_get_x,':pos1x',pos1),
				(position_get_y,':pos1y',pos1),
				(position_get_x,':pos2x',pos2),
				(position_get_y,':pos2y',pos2),
				(position_get_x,':pos3x',pos3),
				(position_get_y,':pos3y',pos3),
				(call_script,'script_agent_get_valid_retreat_position',0,':pos0x',':pos0y',':pos1x',':pos1y',':pos2x',':pos2y',':pos3x',':pos3y'),
				(init_position,pos5),
				(assign,':pos5x',reg0),
				(assign,':pos5y',reg1),
				(position_set_x,pos5,':pos5x'),
				(position_set_y,pos5,':pos5y'),
				(position_set_z_to_ground_level,pos5),
				(agent_set_scripted_destination,':agent_no',pos5,1),
			(try_end),
		]
	),
# rally men
(8.0, 0, 0.0, [], 
	[
		(try_for_agents,':agent_no'),
			(agent_is_alive,':agent_no'),
			(agent_is_human,':agent_no'),
			(agent_get_slot,':morale',':agent_no',slot_agent_morale),
			(val_sub,':morale',10),
			(store_random_in_range,':roll',0,100),
			(lt,':roll',':morale'),
			(agent_clear_scripted_mode,':agent_no'),
		(try_end),
		]
	),
# Player rally command
(0.0, 0, 0.0,
	[
		(key_clicked,key_v),
		(eq,'$g_player_rally_command_used',false),
		],
	[
		(display_message,'@Men, stand firm!'),
		(assign,'$g_player_rally_command_used',true),
		(try_for_agents,':agent_no'),
			(agent_is_alive,':agent_no'),
			(agent_is_human,':agent_no'),
			(agent_get_party_id,':agent_party',':agent_no'),
			(eq,':agent_party','p_main_party'),
			(agent_set_slot,':agent_no',slot_agent_morale,100),
			(agent_set_hit_points,':agent_no',100),
			(agent_clear_scripted_mode,':agent_no'),
		(try_end),
		]
	),
# last ditch charge
(10.0, 0, 0.0, [],
	[
		(get_scene_boundaries,pos2,pos3),
		(try_begin),
			(lt,'$g_player_team_morale',10),
			(try_for_agents,':agent_no'),
				(agent_is_human,':agent_no'),
				(agent_is_alive,':agent_no'),
				(agent_get_team,':agent_team',':agent_no'),
				(eq,':agent_team','$g_player_team'),
				(agent_get_scripted_destination,pos10,':agent_no'),
				(get_distance_between_positions,':dist1',pos10,pos2),
				(get_distance_between_positions,':dist2',pos10,pos3),
				(this_or_next|eq,':dist1',0),
				(eq,':dist2',0),
				(agent_set_slot,':agent_no',slot_agent_morale,100),
				(agent_clear_scripted_mode,':agent_no'),
			(try_end),
		(else_try),
			(lt,'$g_enemy_team_morale',10),
			(try_for_agents,':agent_no'),
				(agent_is_human,':agent_no'),
				(agent_is_alive,':agent_no'),
				(agent_get_team,':agent_team',':agent_no'),
				(eq,':agent_team','$g_enemy_team'),
				(agent_get_scripted_destination,pos10,':agent_no'),
				(get_distance_between_positions,':dist1',pos10,pos2),
				(get_distance_between_positions,':dist2',pos10,pos3),
				(this_or_next|eq,':dist1',0),
				(eq,':dist2',0),
				(agent_set_slot,':agent_no',slot_agent_morale,100),
				(agent_clear_scripted_mode,':agent_no'),
			(try_end),
		(try_end),	
		]
	),
### END MORALE TRIGGERS
### Other command triggers
(0.0, 0, 0.0, [(key_clicked,key_h)],
	[
		(display_message,'@Hold your fire!',lt_green_text),
		(try_for_agents,':agent_no'),
			(agent_is_human,':agent_no'),
			(agent_is_alive,':agent_no'),
			(agent_get_team,':agent_team',':agent_no'),
			(eq,':agent_team','$g_player_team'),
			(agent_get_position,pos20,':agent_no'),
			(agent_set_scripted_destination,':agent_no',pos20),
		(try_end),
		]
	),
		]
	),
   (
    "ai_training",0,-1,
    "You start training.",
    [
     (0,0,0,aif_start_alarmed,2,[]),
#     (1,mtef_no_leader,0,0|aif_start_alarmed,5,[]),
#     (0,mtef_no_leader,0,0|aif_start_alarmed,0,[]),
#     (3,mtef_enemy_party|mtef_reverse_order,0,aif_start_alarmed,6,[]),
#     (4,mtef_enemy_party|mtef_reverse_order,0,aif_start_alarmed,0,[]),
     ],
    [
#      (ti_before_mission_start, 0, 0, [], [(set_rain, 1,100)]),
      (ti_tab_pressed, 0, 0, [],
       [(question_box,"str_give_up_fight")]),
      (ti_question_answered, 0, 0, [],
       [(store_trigger_param_1,":answer"),(eq,":answer",0),(finish_mission,0),]),

     (0, 0, 0,
      [(key_clicked, key_backspace),
       (start_presentation, "prsnt_battle_v2"),
        ], []),

     (0.1, 0, 0,
      [(eq, "$g_presentation_battle_active", 1),
       (call_script, "script_update_order_panel_statistics_and_map"),
        ], []),


      
    ],
  ),
   (
    "camera_test",0,-1,
    "camera Test.",
    [
#     (0,mtef_attackers,0,aif_start_alarmed,5,[]),
     ],
    [
      (1, 0, 0, [(mission_cam_set_mode,1),
          (entry_point_get_position, pos3, 3),
          (mission_cam_set_position, pos3)], []),
#      (ti_before_mission_start, 0, 0, [], [(set_rain, 1,100)]),
      (ti_tab_pressed, 0, 0, [],
       [(finish_mission,0)]),
    ],
  ),

  (
    "village_raid",mtf_battle_mode,charge,
    "You lead your men to battle.",
    [
     (3,mtef_defenders|mtef_team_0,af_override_horse,aif_start_alarmed,12,[]),
     (3,mtef_defenders|mtef_team_0,0,aif_start_alarmed,0,[]),
     (1,mtef_attackers|mtef_team_1,0,aif_start_alarmed,12,[]),
     (1,mtef_attackers|mtef_team_1,0,aif_start_alarmed,0,[]),
     ],
    [
      (ti_tab_pressed, 0, 0, [],
       [
           (try_begin),
             (eq, "$battle_won", 1),
             (call_script, "script_count_mission_casualties_from_agents"),
             (finish_mission,0),
           (else_try),
             (call_script, "script_cf_check_enemies_nearby"),
             (question_box,"str_do_you_want_to_retreat"),
           (else_try),
             (display_message,"str_can_not_retreat"),
           (try_end),
        ]),
      (ti_question_answered, 0, 0, [],
       [(store_trigger_param_1,":answer"),
        (eq,":answer",0),
        (assign, "$pin_player_fallen", 0),
        (call_script, "script_simulate_retreat", 10, 20),
        (call_script, "script_count_mission_casualties_from_agents"),
        (finish_mission,0),]),

      (0, 0, ti_once, [], [(assign,"$battle_won",0),
                           (assign,"$defender_reinforcement_stage",0),
                           (assign,"$attacker_reinforcement_stage",0),
                           (assign, "$g_presentation_battle_active", 0),
                           ]),

      (2, 0, 0, [], [(call_script, "script_check_friendly_kills"),
                     ]),

      (1, 0, 5,
		[
			(lt,"$defender_reinforcement_stage",2),
			(store_mission_timer_a,":mission_time"),
			(ge,":mission_time",10),
			(store_normalized_team_count,":num_defenders", 0),
			(lt,":num_defenders",6),
			],
		[
			(add_reinforcements_to_entry,0,6),
			(val_add,"$defender_reinforcement_stage",1),
			(try_for_agents,':agent_no'),
				(call_script,'script_cf_process_team_reinforcements',':agent_no'),
			(try_end),
			(call_script,'script_update_team_morale'),
			]),
      (1, 0, 5,
		[
			(lt,"$attacker_reinforcement_stage",2),
			(store_mission_timer_a,":mission_time"),
			(ge,":mission_time",10),
			(store_normalized_team_count,":num_attackers", 1),
			(lt,":num_attackers",6),
			],
		[
			(add_reinforcements_to_entry,3,6),
			(val_add,"$attacker_reinforcement_stage",1),
			(try_for_agents,':agent_no'),
				(call_script,'script_cf_process_team_reinforcements',':agent_no'),
			(try_end),
			(call_script,'script_update_team_morale'),
			]),

      
      (1, 60, ti_once, [(store_mission_timer_a,reg1),
                        (ge,reg1,10),(all_enemies_defeated,2),
                        (neg|main_hero_fallen,0),
                        (set_mission_result,1),
                        (assign, "$g_battle_result", 1),
                        (display_message,"str_msg_battle_won"),
                        (assign,"$battle_won",1),
                        ],
           [(call_script, "script_count_mission_casualties_from_agents"),
            (finish_mission,1)]),
      (10, 0, 0, [], [(eq,"$battle_won",1),(display_message,"str_msg_battle_won")]),
      (1, 4, ti_once, [(main_hero_fallen)],
          [
              (assign, "$pin_player_fallen", 1),
              (call_script, "script_simulate_retreat", 10, 20),
              (assign, "$g_battle_result", -1),
              (set_mission_result,-1),
              (call_script, "script_count_mission_casualties_from_agents"),
              (finish_mission,0)]),
      (ti_inventory_key_pressed, 0, 0, [(display_message,"str_use_baggage_for_inventory")], []),

     (0, 0, 0,
      [(key_clicked, key_backspace),
       (start_presentation, "prsnt_battle"),
        ], []),

     (0.1, 0, 0,
      [(eq, "$g_presentation_battle_active", 1),
       (call_script, "script_update_order_panel_statistics_and_map"),
        ], []),

##      #AI Tiggers
##      (0, 0, ti_once, [
##          (store_mission_timer_a,reg1),(ge,reg1,4),
##          (call_script, "script_select_battle_tactic"),
##          (call_script, "script_battle_tactic_init"),
##          ], []),
##      (1, 0, 0, [
##          (store_mission_timer_a,reg1),(ge,reg1,4),
##          (call_script, "script_battle_tactic_apply"),
##          ], []),
# agent morale triggers
# initialize morale
# notes: pos0-pos3 are reserved
# pos0 is player team start position, pos1 is enemy team start position
# pos2 is (0,0) on the scene, pos3 is (maxX,maxY) on the scene)
(0,0,ti_once,[],
	[
		(assign,'$g_player_team_morale',100),
		(assign,'$g_enemy_team_morale',100),
		(assign,'$g_enemy_size',0),
		(assign,'$g_ally_size',0),
		(call_script,'script_clear_temp_array','trp_temp_array_a',250),
		(call_script,'script_clear_temp_array','trp_temp_array_b',250),
		(call_script,'script_clear_temp_array','trp_temp_array_c',250),
		(call_script,'script_clear_temp_array','trp_temp_array_d',250),
		(get_player_agent_no,':player_agent_no'),
		(agent_get_position,pos0,':player_agent_no'),
		(agent_get_team,'$g_player_team',':player_agent_no'),
		(team_set_leader,'$g_player_team',':player_agent_no'),
		(assign,':temp_array_index_a',0),
		(assign,':temp_array_index_c',0),
		(try_for_agents,':agent_no'),
			(agent_is_human,':agent_no'),
			(agent_is_alive,':agent_no'),
			(agent_get_team,':agent_team',':agent_no'),
			(teams_are_enemies,':agent_team','$g_player_team'),
			(assign,'$g_enemy_team',':agent_team'),
			(agent_get_troop_id,':agent_troop_id',':agent_no'),
			(try_begin),
				(troop_is_hero,':agent_troop_id'),
				(troop_set_slot,'trp_temp_array_a',':temp_array_index_a',':agent_troop_id'),
				(troop_set_slot,'trp_temp_array_b',':temp_array_index_a',':agent_no'),
				(val_add,':temp_array_index_a',1),
			(else_try),
				(troop_set_slot,'trp_temp_array_c',':temp_array_index_c',':agent_troop_id'),
				(troop_set_slot,'trp_temp_array_d',':temp_array_index_c',':agent_no'),
				(val_add,':temp_array_index_c',1),
			(try_end),
		(try_end),
		(assign,':skill_level',-1),
		(assign,':temp_array_length_a',':temp_array_index_a'),
		(assign,':temp_array_length_c',':temp_array_index_c'),
		(try_for_range,':temp_array_index',0,':temp_array_length_a'),
			(troop_get_slot,':agent_troop_id','trp_temp_array_a',':temp_array_index'),
			(store_skill_level,':leadership_skill',skl_leadership,':agent_troop_id'),
			(gt,':leadership_skill',':skill_level'),
			(assign,':skill_level',':leadership_skill'),
			(troop_get_slot,':agent_no','trp_temp_array_b',':temp_array_index'),
			(team_set_leader,'$g_enemy_team',':agent_no'),
		(try_end),
		(try_begin),
			(eq,':skill_level',-1),
			(try_for_range,':temp_array_index',0,':temp_array_length_c'),
				(troop_get_slot,':agent_troop_id','trp_temp_array_c',':temp_array_index'),
				(store_character_level,':leadership_skill',':agent_troop_id'),
				(gt,':leadership_skill',':skill_level'),
				(assign,':skill_level',':leadership_skill'),
				(troop_get_slot,':agent_no','trp_temp_array_d',':temp_array_index'),
				(team_set_leader,'$g_enemy_team',':agent_no'),
			(try_end),
		(try_end),
		(team_get_leader,':enemy_leader','$g_enemy_team'),
		(agent_get_position,pos1,':enemy_leader'),
		(try_for_agents,':agent_no'),
			(agent_is_human,':agent_no'),
			(agent_is_alive,':agent_no'),
			(agent_get_party_id,':agent_party',':agent_no'),
			(try_begin),
				(this_or_next|eq,':agent_party','p_main_party'),
				(agent_is_ally,':agent_no'),
				(agent_set_team,':agent_no','$g_player_team'),
				(store_agent_hit_points,':agent_hp',':agent_no'),
				(call_script,'script_object_modify_slot',obj_agent,':agent_no',slot_agent_morale,op_asn,':agent_hp'),
				# (call_script,'script_agent_get_level',':agent_no'),
				# (assign,':agent_level',reg0),
				# (store_mul,':morale',':agent_hp',':agent_level'),
				(val_add,'$g_ally_size',1),
			(else_try),
				(store_agent_hit_points,':agent_hp',':agent_no'),
				(agent_set_team,':agent_no','$g_enemy_team'),
				(call_script,'script_object_modify_slot',obj_agent,':agent_no',slot_agent_morale,op_asn,':agent_hp'),
				# (call_script,'script_agent_get_level',':agent_no'),
				# (assign,':agent_level',reg0),
				# (store_mul,':morale',':agent_hp',':agent_level'),
				(val_add,'$g_enemy_size',1),
			(try_end),
		(try_end),
		(assign,reg10,'$g_ally_size'),
		(assign,reg11,'$g_enemy_size'),
		(display_message,'@{reg10} allies, {reg11} enemies',lt_blue_text),
		]
	),
# update morale
(1.0, 0, 0.0, [],
	[
		(call_script,'script_update_team_morale'),
		]
	),
# morale check
(8.0, 0, 0.0, [],
	[
		(get_scene_boundaries,pos2,pos3),
		(try_begin),
			(lt,'$g_player_team_morale',80),
			(try_for_agents,':agent_no'),
				(agent_is_human,':agent_no'),
				(agent_is_alive,':agent_no'),
				(agent_get_team,':agent_team',':agent_no'),
				(eq,':agent_team','$g_player_team'),
				(call_script,'script_cf_agent_check_morale','$g_player_team_morale'),
				(get_distance_between_positions,':dist1',pos0,pos2),
				(get_distance_between_positions,':dist2',pos0,pos3),
				(try_begin),
					(gt,':dist1',':dist2'),
					(agent_set_scripted_destination,':agent_no',pos3,1),
				(else_try),
					(agent_set_scripted_destination,':agent_no',pos2,1),
				(try_end),
			(try_end),
		(else_try),
			(lt,'$g_enemy_team_morale',80),
			(try_for_agents,':agent_no'),
				(agent_is_human,':agent_no'),
				(agent_is_alive,':agent_no'),
				(agent_get_team,':agent_team',':agent_no'),
				(eq,':agent_team','$g_enemy_team'),
				(call_script,'script_cf_agent_check_morale','$g_enemy_team_morale'),
				(get_distance_between_positions,':dist1',pos1,pos2),
				(get_distance_between_positions,':dist2',pos1,pos3),
				(try_begin),
					(gt,':dist1',':dist2'),
					(agent_set_scripted_destination,':agent_no',pos3,1),
				(else_try),
					(agent_set_scripted_destination,':agent_no',pos2,1),
				(try_end),
			(try_end),
		(try_end),
		(assign,reg10,'$g_player_team_morale'),
		(assign,reg11,'$g_enemy_team_morale'),
		(str_store_string,s10,'@Player team'),
		(str_store_string,s11,'@Enemy team'),
		(display_message,'@{s10} morale: {reg10}; {s11} morale: {reg11}',debug_text),
		]
	),
# (7.5, 0, 0.0, [],
	# [
		# (team_get_leader,':player_team_leader','$g_player_team'),
		# (team_get_leader,':enemy_team_leader','$g_enemy_team'),
		# (agent_get_troop_id,':player_team_leader_troop_id',':player_team_leader'),
		# (agent_get_troop_id,':enemy_team_leader_troop_id',':enemy_team_leader'),
		# (store_skill_level,':player_team_leadership',skl_leadership,':player_team_leader_troop_id'),
		# (store_skill_level,':enemy_team_leadership',skl_leadership,':enemy_team_leader_troop_id'),
		# (agent_get_position,pos10,':player_team_leader'),
		# (agent_get_position,pos11,':enemy_team_leader'),
		# (try_for_agents,':agent_no'),
			# (agent_is_human,':agent_no'),
			# (agent_is_alive,':agent_no'),
			# (agent_get_team,':agent_team',':agent_no'),
			# (agent_get_position,pos12,':agent_no'),
			# (try_begin),
				# (eq,':agent_team','$g_player_team'),
				# (get_distance_between_positions,':dist',pos10,pos12),
				# (try_begin),
					# (is_between,':dist',0,1000),
					# (assign,':multiplier',4),
				# (else_try),
					# (is_between,':dist',1000,5000),
					# (assign,':multiplier',2),
				# (else_try),
					# (assign,':multiplier',1),
				# (try_end),
				# (store_mul,':morale_bonus',':player_team_leadership',':multiplier'),
			# (else_try),
				# (eq,':agent_team','$g_enemy_team'),
				# (get_distance_between_positions,':dist',pos11,pos12),
				# (try_begin),
					# (is_between,':dist',0,1000),
					# (assign,':multiplier',4),
				# (else_try),
					# (is_between,':dist',1000,5000),
					# (assign,':multiplier',2),
				# (else_try),
					# (assign,':multiplier',1),
				# (try_end),
				# (store_mul,':morale_bonus',':enemy_team_leadership',':multiplier'),
			# (try_end),
			# (call_script,'script_object_modify_slot',obj_agent,':agent_no',slot_agent_morale,op_add,':morale_bonus'),
		# (try_end),
		# ]
	# ),
# routing check
(4.0, 0, 0.0, [],
	[
		(store_sub,':morale_diff','$g_player_team_morale','$g_enemy_team_morale'),
		(get_scene_boundaries,pos2,pos3),
		(try_begin),
			(le,':morale_diff',-40),
			(display_message,'@Your men have been routed!',red_text),
			(try_for_agents,':agent_no'),
				(agent_is_human,':agent_no'),
				(agent_is_alive,':agent_no'),
				(agent_get_team,':agent_team',':agent_no'),
				(eq,':agent_team','$g_player_team'),
				(call_script,'script_cf_agent_check_rout',':agent_no'),
				(get_distance_between_positions,':dist1',pos0,pos2),
				(get_distance_between_positions,':dist2',pos0,pos3),
				(try_begin),
					(gt,':dist1',':dist2'),
					(agent_set_scripted_destination,':agent_no',pos3,1),
				(else_try),
					(agent_set_scripted_destination,':agent_no',pos2,1),
				(try_end),
			(try_end),
		(else_try),
			(ge,':morale_diff',40),
			(display_message,'@Your enemy has been routed!',lt_red_text),
			(try_for_agents,':agent_no'),
				(agent_is_human,':agent_no'),
				(agent_is_alive,':agent_no'),
				(agent_get_team,':agent_team',':agent_no'),
				(eq,':agent_team','$g_enemy_team'),
				(call_script,'script_cf_agent_check_rout',':agent_no'),
				(get_distance_between_positions,':dist1',pos1,pos2),
				(get_distance_between_positions,':dist2',pos1,pos3),
				(try_begin),
					(gt,':dist1',':dist2'),
					(agent_set_scripted_destination,':agent_no',pos3,1),
				(else_try),
					(agent_set_scripted_destination,':agent_no',pos2,1),
				(try_end),
			(try_end),
		]
	),
# rally men
(8.0, 0, 0.0, [], 
	[
		(try_for_agents,':agent_no'),
			(agent_is_alive,':agent_no'),
			(agent_is_human,':agent_no'),
			(agent_get_slot,':morale',':agent_no',slot_agent_morale),
			(val_sub,':morale',10),
			(store_random_in_range,':roll',0,100),
			(lt,':roll',':morale'),
			(agent_clear_scripted_mode,':agent_no'),
		(try_end),
		]
	),
# last ditch charge
(10.0, 0, 0.0, [],
	[
		(get_scene_boundaries,pos2,pos3),
		(try_begin),
			(lt,'$g_player_team_morale',10),
			(try_for_agents,':agent_no'),
				(agent_is_human,':agent_no'),
				(agent_is_alive,':agent_no'),
				(agent_get_team,':agent_team',':agent_no'),
				(eq,':agent_team','$g_player_team'),
				(agent_get_scripted_destination,pos10,':agent_no'),
				(get_distance_between_positions,':dist1',pos10,pos2),
				(get_distance_between_positions,':dist2',pos10,pos3),
				(this_or_next|eq,':dist1',0),
				(eq,':dist2',0),
				(agent_set_slot,':agent_no',slot_agent_morale,100),
				(agent_clear_scripted_mode,':agent_no'),
			(try_end),
		(else_try),
			(lt,'$g_enemy_team_morale',10),
			(try_for_agents,':agent_no'),
				(agent_is_human,':agent_no'),
				(agent_is_alive,':agent_no'),
				(agent_get_team,':agent_team',':agent_no'),
				(eq,':agent_team','$g_enemy_team'),
				(agent_get_scripted_destination,pos10,':agent_no'),
				(get_distance_between_positions,':dist1',pos10,pos2),
				(get_distance_between_positions,':dist2',pos10,pos3),
				(this_or_next|eq,':dist1',0),
				(eq,':dist2',0),
				(agent_set_slot,':agent_no',slot_agent_morale,100),
				(agent_clear_scripted_mode,':agent_no'),
			(try_end),
		(try_end),	
		]
	),
### END MORALE TRIGGERS
    ],
  ),
##########################################################################################
#
# Onin no Ran MTs
#
##########################################################################################
('animation_test',0,-1,
	'Enter',
	[
		(0,mtef_scene_source|mtef_team_1,0,0,1,[]),
		(1,mtef_scene_source|mtef_team_1,0,0,1,[]),
		],
	[
		(ti_inventory_key_pressed, 0, 0.0, [],
			[
				(assign,':prop_position_no',0),
				#(assign,':ep_position_no',0),
				(scene_prop_get_num_instances,':num_instances','spr_banner_hosokawa'),
				(assign,reg41,':num_instances'),
				(display_message,'str_reg41',pink_text),
				(scene_prop_get_instance,':spr_instance','spr_banner_hosokawa',0),
				(assign,reg41,':spr_instance'),
				(display_message,'str_reg41',lt_green_text),
				(prop_instance_get_starting_position,':prop_position_no',':spr_instance','spr_banner_hosokawa'),
				(assign,reg41,':prop_position_no'),
				(display_message,'str_reg41',lt_blue_text),
				(position_get_x,reg41,':prop_position_no'),
				(display_message,'str_reg41',red_text),
				(position_get_y,reg41,':prop_position_no'),
				(display_message,'str_reg41',blue_text),
				(position_get_z,reg41,':prop_position_no'),
				(display_message,'str_reg41',green_text),
				(assign,':new_prop_position_no',3),
				#(entry_point_get_position,':ep_position_no',1),
				#(assign,reg41,':ep_position_no'),
				#(display_message,'str_reg41',cyan_text),
				(init_position,':new_prop_position_no'),
				(copy_position,':new_prop_position_no',':prop_position_no'),
				(position_move_x,':new_prop_position_no',1000,0),
				(position_get_x,reg41,':new_prop_position_no'),
				(display_message,'str_reg41',red_text),
				(get_distance_between_positions,reg41,':prop_position_no',':new_prop_position_no'),
				(display_message,'str_reg41',purple_text),
				(prop_instance_animate_to_position,':spr_instance',':new_prop_position_no',500),
				],
		),
		(ti_tab_pressed, 0, 0.0, [(set_trigger_result,1)],[]),
		]
	),
(
    "ikq_fight",mtf_battle_mode,-1,
    "You lead your men to battle.",
    [
     (1,mtef_leader_only,0,0,1,[]),
     (1,mtef_no_leader,0,aif_start_alarmed,5,[]),
     (0,mtef_no_leader,0,aif_start_alarmed,0,[]),
     (3,mtef_enemy_party|mtef_reverse_order,0,aif_start_alarmed,6,[]),
     (4,mtef_enemy_party|mtef_reverse_order,0,aif_start_alarmed,0,[]),
     ],
    [
        (ti_tab_pressed, 0, 0, [],
         [
           (try_begin),
             (eq, "$battle_won", 1),
             (finish_mission,0),
           (else_try),
             (call_script, "script_cf_check_enemies_nearby"),
             (question_box,"str_do_you_want_to_retreat",'str_yes','str_no'),
           (else_try),
             (display_message,"str_can_not_retreat"),
           (try_end),
           ]),
        (ti_question_answered, 0, 0, [],
         [(store_trigger_param_1,":answer"),(eq,":answer",0),(finish_mission,0),]),
        (ti_inventory_key_pressed, 0, 0, [(display_message,"str_use_baggage_for_inventory")], []),
      (0, 0, ti_once, [], [(assign,"$battle_won",0),
                           (assign,"$enemy_reinforcement_stage",0),
                           (assign,"$friend_reinforcement_stage",0)]),
      (1, 0, 5, [(lt,"$enemy_reinforcement_stage",2),(store_mission_timer_a,reg1),(ge,reg1,10),(store_enemy_count,reg(2)),(lt,reg(2),3)],
           [(add_reinforcements_to_entry,4,8),(val_add,"$enemy_reinforcement_stage",1)]),
      (1, 0, 5, [(lt,"$friend_reinforcement_stage",2),(store_mission_timer_a,reg1),(ge,reg1,10),(store_friend_count,reg(2)),(lt,reg(2),3)],
           [(add_reinforcements_to_entry,2,8),(val_add,"$friend_reinforcement_stage",1)]),
      (1, 60, ti_once, [(store_mission_timer_a,reg1),
                        (ge,reg1,10),
                        (all_enemies_defeated,2),
                        (neg|main_hero_fallen,0),
                        (party_get_num_companions,":opp_size","$g_encountered_party"),
						(store_faction_of_party,":opp_fac","$g_encountered_party"),
						(party_get_num_companions,":plyr_size","p_main_party"),
                        (call_script,'script_post_battle_cleanup'),
                        (call_script,"script_assign_fame",':opp_size',':opp_fac',':plyr_size'),
                        (call_script,"script_award_xp",':opp_size'),
                        (call_script,"script_assign_fp",':opp_size',':opp_fac'),
                        (set_mission_result,1),
                        (assign, "$g_battle_result",1),
                        (display_message,"str_msg_battle_won"),
                        (assign,"$battle_won",1),
                        (jump_to_menu,"mnu_ikq_leave")],
           [(finish_mission,1)]),
      (10, 0, 0, [], [(eq,"$battle_won",1),(display_message,"str_msg_battle_won")]),
      (1, 4, ti_once, [(main_hero_fallen,0)],
          [(assign, "$g_battle_result", -1),(set_mission_result,-1),(finish_mission,1)]),
	#AI Tiggers
      (0, 2, ti_once, [
          (store_mission_timer_a,":mission_time"),(ge,":mission_time",1),
          ],
       [(call_script, "script_select_battle_tactic"),
        (call_script, "script_battle_tactic_init")]),
      
      (1, 0, 0, [
          (store_mission_timer_a,":mission_time"),(ge,":mission_time",2),
          (call_script, "script_battle_tactic_apply"),
          ], []),

     (0, 0, 0,
      [(key_clicked, key_backspace),
       (start_presentation, "prsnt_battle"),
        ], []),

     (0.1, 0, 0,
		[
			(eq, "$g_presentation_battle_active", 1),
			(call_script, "script_update_order_panel_statistics_and_map"),
			],
		[]),
		
	(ti_before_mission_start,0,0.0,[],
	[
		(assign,"$player_mission_kills",0),
		]
	),

(ti_once, 0, 0, [],
	[
		(get_player_agent_no,':player_agent_no'),
		(store_agent_hit_points,'$player_mission_health',':player_agent_no'),
		]
	),

# weapons and armor durability code
(0.2, 0, 0, [],
	[
		(get_player_agent_kill_count,":player_kills",0),
		(assign, ":past_kills", "$player_mission_kills"),  #player_mission_kills is set to 0 whenever a mission starts.
		(assign, "$player_mission_kills", ":player_kills"),
		(get_player_agent_no,":player_agent_no"),
		(try_begin),
			(gt, ":player_kills", ":past_kills"),
			(agent_get_wielded_item,":wielded_item",":player_agent_no",0),
			(item_get_slot,':weapon_type',':wielded_item',slot_item_type),
			(try_begin),
				(eq,':weapon_type',sit_metal_weapon),
				(store_random_in_range,':weapon_damage',1,6),
				(val_add,':weapon_damage',3),
				(call_script,'script_modify_item_durability',':wielded_item',':weapon_damage'),
			(else_try),
				(eq,':weapon_type',sit_wooden_weapon),
				(store_random_in_range,':weapon_damage',0,5),
				(val_add,':weapon_damage',1),
				(call_script,'script_modify_item_durability',':wielded_item',':weapon_damage'),
			(else_try),
				(eq,':weapon_type',sit_metal_weapon),
				(store_random_in_range,':weapon_damage',1,7),
				(val_add,':weapon_damage',3),
				(call_script,'script_modify_item_durability',':wielded_item',':weapon_damage'),
			(else_try),
				(eq,':weapon_type',sit_ranged_weapon),
				(store_random_in_range,':weapon_damage',0,4),
				(val_add,':weapon_damage',2),
				(call_script,'script_modify_item_durability',':wielded_item',':weapon_damage'),
			(try_end),
		(try_end),
		(store_agent_hit_points,':current_health',':player_agent_no'),
		(try_begin),
			(lt,':current_health','$player_mission_health'),
			(assign,'$player_mission_health',':current_health'),
			# armor
			(try_begin),
				(troop_get_inventory_slot,':player_armor','trp_player',ek_body),
				(gt,':player_armor',0),
				(store_random_in_range,':armor_damage',0,6),
				(val_add,':armor_damage',3),
				(call_script,'script_modify_item_durability',':player_armor',':weapon_damage'),
			(try_end),
			# helmet
			(try_begin),
				(troop_get_inventory_slot,':player_helmet','trp_player',ek_head),
				(gt,':player_helmet',0),
				(store_random_in_range,':armor_damage',0,4),
				(val_add,':armor_damage',2),
				(call_script,'script_modify_item_durability',':player_helmet',':weapon_damage'),
			(try_end),
			# boots
			(try_begin),
				(troop_get_inventory_slot,':player_boots','trp_player',ek_foot),
				(gt,':player_boots',0),
				(store_random_in_range,':armor_damage',0,6),
				(val_add,':armor_damage',1),
				(call_script,'script_modify_item_durability',':player_boots',':weapon_damage'),
			(try_end),
			# gloves
			(try_begin),
				(troop_get_inventory_slot,':player_gloves','trp_player',ek_gloves),
				(gt,':player_gloves',0),
				(store_random_in_range,':armor_damage',0,3),
				(call_script,'script_modify_item_durability',':player_gloves',':weapon_damage'),
			(try_end),
		(try_end),
		]
	),
	# archers find high ground command
# (0.0, 0, 0.0, [(eq,1,0),(key_clicked,key_h)],
	# [
		# (assign,':end_loop',false),
		# (get_player_agent_no,':player_agent_no'),
		# (agent_get_team,':player_team',':player_agent_no'),
		# (agent_get_position,pos1,':player_agent_no'),
		# (set_fixed_point_multiplier,100),
		# (get_scene_boundaries,pos60,pos61),
		# (position_get_x,':min_x',pos60),
		# (position_get_y,':min_y',pos60),
		# (position_get_x,':max_x',pos61),
		# (position_get_y,':max_y',pos61),
		# (try_for_range,':unused',0,1000),
			# (eq,':end_loop',false),
			#set positions to pos1
			# (copy_position,pos2,pos1),
			# (copy_position,pos3,pos1),
			# (copy_position,pos4,pos1),
			# (copy_position,pos5,pos1),
			# (call_script,'script_position_add_y',pos2,100,true,':min_y',':max_y'),
			# (call_script,'script_position_add_x',pos3,100,true,':min_x',':max_x'),
			# (call_script,'script_position_add_y',pos4,-100,true,':min_y',':max_y'),
			# (call_script,'script_position_add_x',pos5,-100,true,':min_x',':max_x'),
			#set all positions to ground level and get z values
			# (position_set_z_to_ground_level,pos2),
			# (position_set_z_to_ground_level,pos3),
			# (position_set_z_to_ground_level,pos4),
			# (position_set_z_to_ground_level,pos5),
			# (position_get_z,':pos1_z',pos1),
			# (position_get_z,':pos2_z',pos2),
			# (position_get_z,':pos3_z',pos3),
			# (position_get_z,':pos4_z',pos4),
			# (position_get_z,':pos5_z',pos5),
			#compare z values
			# (store_sub,':dz_21',':pos2_z',':pos1_z'),
			# (store_sub,':dz_31',':pos3_z',':pos1_z'),
			# (store_sub,':dz_41',':pos4_z',':pos1_z'),
			# (store_sub,':dz_51',':pos5_z',':pos1_z'),
			# (try_begin),
				#all values are negative, pos1 at a local maximum
				# (le,':dz_21',0),
				# (le,':dz_31',0),
				# (le,':dz_41',0),
				# (le,':dz_51',0),
				# (assign,':end_loop',true),
			# (else_try),
				# (assign,':dz_21_temp',':dz_21'),
				# (assign,':dz_31_temp',':dz_31'),
				# (assign,':dz_41_temp',':dz_41'),
				# (assign,':dz_51_temp',':dz_51'),
				# (val_max,':dz_21_temp',':dz_31_temp'),
				# (val_max,':dz_21_temp',':dz_41_temp'),
				# (val_max,':dz_21_temp',':dz_41_temp'),
				# (try_begin),
					# (eq,':dz_21_temp',':dz_21'),
					# (copy_position,pos1,pos2),
				# (else_try),
					# (eq,':dz_31_temp',':dz_31'),
					# (copy_position,pos1,pos3),
				# (else_try),
					# (eq,':dz_41_temp',':dz_41'),
					# (copy_position,pos1,pos4),
				# (else_try),
					# (eq,':dz_51_temp',':dz_51'),
					# (copy_position,pos1,pos5),
				# (try_end),
			# (try_end),
		# (try_end),
		# (team_set_order_position,':player_team',grc_archers,pos1),
		# (team_give_order,':player_team',grc_archers,mordr_hold),
		# (display_message,'@Archers, take high ground!',purple_text),
		# ]
	# ),
    ],
  ),
  (
    "army_fight",mtf_battle_mode,-1,
    "You lead your men to battle.",
    [
     (1,mtef_leader_only,0,0,1,[]),
     (1,mtef_no_leader,0,aif_start_alarmed,5,[]),
     (0,mtef_no_leader,0,aif_start_alarmed,0,[]),
     (3,mtef_enemy_party|mtef_reverse_order,0,aif_start_alarmed,6,[]),
     (4,mtef_enemy_party|mtef_reverse_order,0,aif_start_alarmed,0,[]),
     ],
    [
        (ti_tab_pressed, 0, 0, [],
         [
           (try_begin),
             (eq, "$battle_won", 1),
             (finish_mission,1),
           (else_try),
             (call_script, "script_cf_check_enemies_nearby"),
             (question_box,"str_do_you_want_to_retreat",'str_yes','str_no'),
           (else_try),
             (display_message,"str_can_not_retreat"),
           (try_end),
           ]),
        (ti_question_answered, 0, 0, [],
         [(store_trigger_param_1,":answer"),(eq,":answer",0),(finish_mission,0),]),
        (ti_inventory_key_pressed, 0, 0, [(display_message,"str_use_baggage_for_inventory")], []),
      (0, 0, ti_once, [], [(assign,"$battle_won",0),
                           (assign,"$enemy_reinforcement_stage",0),
                           (assign,"$friend_reinforcement_stage",0)]),
      (1, 0, 5, [(lt,"$enemy_reinforcement_stage",2),(store_mission_timer_a,reg1),(ge,reg1,10),(store_enemy_count,reg(2)),(lt,reg(2),3)],
           [(add_reinforcements_to_entry,4,8),(val_add,"$enemy_reinforcement_stage",1)]),
      (1, 0, 5, [(lt,"$friend_reinforcement_stage",2),(store_mission_timer_a,reg1),(ge,reg1,10),(store_friend_count,reg(2)),(lt,reg(2),3)],
           [(add_reinforcements_to_entry,2,8),(val_add,"$friend_reinforcement_stage",1)]),
      (1, 60, ti_once, [(store_mission_timer_a,reg1),
                        (ge,reg1,10),
                        (all_enemies_defeated,2),
                        (neg|main_hero_fallen,0),
						(party_get_num_companions,":opp_size","$g_encountered_party"),
						(store_faction_of_party,":opp_fac","$g_encountered_party"),
						(party_get_num_companions,":plyr_size","p_main_party"),
                        (call_script,'script_post_battle_cleanup'),
                        (call_script,"script_assign_fame",':opp_size',':opp_fac',':plyr_size'),
                        (call_script,"script_award_xp",':opp_size'),
                        (call_script,"script_assign_fp",':opp_size',':opp_fac'),
                        (set_mission_result,1),
                        (assign, "$g_battle_result",1),
                        (display_message,"str_msg_battle_won"),
                        (assign,"$battle_won",1)],
           [(finish_mission,1)]),
      (10, 0, 0, [], [(eq,"$battle_won",1),(display_message,"str_msg_battle_won")]),
      (1, 4, ti_once, [(main_hero_fallen,0)],
          [(assign, "$g_battle_result", -1),(set_mission_result,-1),(finish_mission,1)]),
	#AI Tiggers
      (0, 2, ti_once, [
          (store_mission_timer_a,":mission_time"),(ge,":mission_time",1),
          ],
       [(call_script, "script_select_battle_tactic"),
        (call_script, "script_battle_tactic_init")]),
      
      (1, 0, 0, [
          (store_mission_timer_a,":mission_time"),(ge,":mission_time",2),
          (call_script, "script_battle_tactic_apply"),
          ], []),

     (0, 0, 0,
      [(key_clicked, key_backspace),
       (start_presentation, "prsnt_battle"),
        ], []),

     (0.1, 0, 0,
      [(eq, "$g_presentation_battle_active", 1),
       (call_script, "script_update_order_panel_statistics_and_map"),
        ], []),
		 
	(ti_before_mission_start,0,0.0,[],
	[
		(assign,"$player_mission_kills",0),
		]
	),

(ti_once, 0, 0, [],
	[
		(get_player_agent_no,':player_agent_no'),
		(store_agent_hit_points,'$player_mission_health',':player_agent_no'),
		]
	),

# weapons and armor durability code
(0.2, 0, 0, [],
	[
		(get_player_agent_kill_count,":player_kills",0),
		(assign, ":past_kills", "$player_mission_kills"),  #player_mission_kills is set to 0 whenever a mission starts.
		(assign, "$player_mission_kills", ":player_kills"),
		(get_player_agent_no,":player_agent_no"),
		(try_begin),
			(gt, ":player_kills", ":past_kills"),
			(agent_get_wielded_item,":wielded_item",":player_agent_no",0),
			(item_get_slot,':weapon_type',':wielded_item',slot_item_type),
			(try_begin),
				(eq,':weapon_type',sit_metal_weapon),
				(store_random_in_range,':weapon_damage',1,6),
				(val_add,':weapon_damage',3),
				(call_script,'script_modify_item_durability',':wielded_item',':weapon_damage'),
			(else_try),
				(eq,':weapon_type',sit_wooden_weapon),
				(store_random_in_range,':weapon_damage',0,5),
				(val_add,':weapon_damage',1),
				(call_script,'script_modify_item_durability',':wielded_item',':weapon_damage'),
			(else_try),
				(eq,':weapon_type',sit_metal_weapon),
				(store_random_in_range,':weapon_damage',1,7),
				(val_add,':weapon_damage',3),
				(call_script,'script_modify_item_durability',':wielded_item',':weapon_damage'),
			(else_try),
				(eq,':weapon_type',sit_ranged_weapon),
				(store_random_in_range,':weapon_damage',0,4),
				(val_add,':weapon_damage',2),
				(call_script,'script_modify_item_durability',':wielded_item',':weapon_damage'),
			(try_end),
		(try_end),
		(store_agent_hit_points,':current_health',':player_agent_no'),
		(try_begin),
			(lt,':current_health','$player_mission_health'),
			(assign,'$player_mission_health',':current_health'),
			# armor
			(try_begin),
				(troop_get_inventory_slot,':player_armor','trp_player',ek_body),
				(gt,':player_armor',0),
				(store_random_in_range,':armor_damage',0,6),
				(val_add,':armor_damage',3),
				(call_script,'script_modify_item_durability',':player_armor',':weapon_damage'),
			(try_end),
			# helmet
			(try_begin),
				(troop_get_inventory_slot,':player_helmet','trp_player',ek_head),
				(gt,':player_helmet',0),
				(store_random_in_range,':armor_damage',0,4),
				(val_add,':armor_damage',2),
				(call_script,'script_modify_item_durability',':player_helmet',':weapon_damage'),
			(try_end),
			# boots
			(try_begin),
				(troop_get_inventory_slot,':player_boots','trp_player',ek_foot),
				(gt,':player_boots',0),
				(store_random_in_range,':armor_damage',0,6),
				(val_add,':armor_damage',1),
				(call_script,'script_modify_item_durability',':player_boots',':weapon_damage'),
			(try_end),
			# gloves
			(try_begin),
				(troop_get_inventory_slot,':player_gloves','trp_player',ek_gloves),
				(gt,':player_gloves',0),
				(store_random_in_range,':armor_damage',0,3),
				(call_script,'script_modify_item_durability',':player_gloves',':weapon_damage'),
			(try_end),
		(try_end),
		]
	),
	# archers find high ground command
# (0.0, 0, 0.0, [(key_clicked,key_h)],
	# [
		# (assign,':end_loop',false),
		# (get_player_agent_no,':player_agent_no'),
		# (agent_get_team,':player_team',':player_agent_no'),
		# (agent_get_position,pos1,':player_agent_no'),
		# (set_fixed_point_multiplier,100),
		# (get_scene_boundaries,pos60,pos61),
		# (position_get_x,':min_x',pos60),
		# (position_get_y,':min_y',pos60),
		# (position_get_x,':max_x',pos61),
		# (position_get_y,':max_y',pos61),
		# (try_for_range,':unused',0,1000),
			# (eq,':end_loop',false),
			# set positions to pos1
			# (copy_position,pos2,pos1),
			# (copy_position,pos3,pos1),
			# (copy_position,pos4,pos1),
			# (copy_position,pos5,pos1),
			# (call_script,'script_position_add_y',pos2,100,true,':min_y',':max_y'),
			# (call_script,'script_position_add_x',pos3,100,true,':min_x',':max_x'),
			# (call_script,'script_position_add_y',pos4,-100,true,':min_y',':max_y'),
			# (call_script,'script_position_add_x',pos5,-100,true,':min_x',':max_x'),
			# set all positions to ground level and get z values
			# (position_set_z_to_ground_level,pos2),
			# (position_set_z_to_ground_level,pos3),
			# (position_set_z_to_ground_level,pos4),
			# (position_set_z_to_ground_level,pos5),
			# (position_get_z,':pos1_z',pos1),
			# (position_get_z,':pos2_z',pos2),
			# (position_get_z,':pos3_z',pos3),
			# (position_get_z,':pos4_z',pos4),
			# (position_get_z,':pos5_z',pos5),
			# compare z values
			# (store_sub,':dz_21',':pos2_z',':pos1_z'),
			# (store_sub,':dz_31',':pos3_z',':pos1_z'),
			# (store_sub,':dz_41',':pos4_z',':pos1_z'),
			# (store_sub,':dz_51',':pos5_z',':pos1_z'),
			# (try_begin),
				# all values are negative, pos1 at a local maximum
				# (le,':dz_21',0),
				# (le,':dz_31',0),
				# (le,':dz_41',0),
				# (le,':dz_51',0),
				# (assign,':end_loop',true),
			# (else_try),
				# (assign,':dz_21_temp',':dz_21'),
				# (assign,':dz_31_temp',':dz_31'),
				# (assign,':dz_41_temp',':dz_41'),
				# (assign,':dz_51_temp',':dz_51'),
				# (val_max,':dz_21_temp',':dz_31_temp'),
				# (val_max,':dz_21_temp',':dz_41_temp'),
				# (val_max,':dz_21_temp',':dz_41_temp'),
				# (try_begin),
					# (eq,':dz_21_temp',':dz_21'),
					# (copy_position,pos1,pos2),
				# (else_try),
					# (eq,':dz_31_temp',':dz_31'),
					# (copy_position,pos1,pos3),
				# (else_try),
					# (eq,':dz_41_temp',':dz_41'),
					# (copy_position,pos1,pos4),
				# (else_try),
					# (eq,':dz_51_temp',':dz_51'),
					# (copy_position,pos1,pos5),
				# (try_end),
			# (try_end),
		# (try_end),
		# (team_set_order_position,':player_team',grc_archers,pos1),
		# (team_give_order,':player_team',grc_archers,mordr_hold),
		# (display_message,'@Archers, take high ground!',purple_text),
		# ]
	# ),
    ],
  ),
  (
    "dojo_training_fight",mtf_arena_fight,-1,
    "You will fight a match in the dojo.",
    [
      (0,mtef_visitor_source|mtef_team_1,af_override_all,aif_start_alarmed,1,[itm_bokken,itm_kataginu_j]),
      (1,mtef_visitor_source|mtef_team_2,af_override_all,aif_start_alarmed,1,[itm_hanbo,itm_kataginu_j]),
      (2,mtef_visitor_source|mtef_team_3,af_override_all,aif_start_alarmed,1,[itm_practice_naginata,itm_kataginu_j]),
      (3,mtef_visitor_source|mtef_team_4,af_override_all,aif_start_alarmed,1,[itm_bokken,itm_kataginu_j]),
    ],
    [
        (1, 3, ti_once, [(main_hero_fallen,0),(assign,"$waiting_for_training_fight_result",0)],
         [(jump_to_scene,"$dojo_scene")]),
        (ti_tab_pressed, 0, 0, [],
         [
           (try_begin),
             (eq, "$training_fight_won", 1),
             (finish_mission,0),
           (else_try),
             (call_script, "script_cf_check_enemies_nearby"),
             (question_box,"str_do_you_want_to_retreat",'str_yes','str_no'),
           (else_try),
             (display_message,"str_can_not_retreat"),
           (try_end),
           ]),
      (1, 3, ti_once, [(store_mission_timer_a,reg1),
                       (ge,reg1,1),
                       (num_active_teams_le,1),
                       (neg|main_hero_fallen,0),
                       (assign,"$waiting_for_training_fight_result",0),
                       (assign,"$training_fight_won",1)],
       [(jump_to_scene,"$dojo_scene")]),
    ],
  ),
  (
      "kengo_training_fight",mtf_arena_fight,-1,
      "You will fight a match in the training field.",
      [
          (0,mtef_scene_source|mtef_team_1,af_override_all,aif_start_alarmed,1,[itm_bokken,itm_kataginu_j]),
          (1,mtef_scene_source|mtef_team_1,af_override_all,aif_start_alarmed,1,[itm_bo,itm_kataginu_j]),
          (2,mtef_scene_source|mtef_team_1,af_override_all,aif_start_alarmed,1,[itm_practice_naginata,itm_kataginu_j]),
          (3,mtef_scene_source|mtef_team_1,af_override_all,aif_start_alarmed,1,[itm_light_yumi,itm_ya,itm_kataginu_j]),
          (4,mtef_scene_source|mtef_team_1,af_override_all,aif_start_alarmed,1,[itm_kodachi,itm_shaken_shuriken,itm_kataginu_j]),
          (4,mtef_visitor_source|mtef_team_2,af_override_all,aif_start_alarmed,1,[itm_bokken,itm_kataginu_j]),
          (5,mtef_visitor_source|mtef_team_2,af_override_all,aif_start_alarmed,1,[itm_bokken,itm_kataginu_j]),
          (6,mtef_visitor_source|mtef_team_2,af_override_all,aif_start_alarmed,1,[itm_bokken,itm_kataginu_j]),
          (7,mtef_visitor_source|mtef_team_2,af_override_all,aif_start_alarmed,1,[itm_bokken,itm_kataginu_j]),
          (8,mtef_visitor_source|mtef_team_2,af_override_all,aif_start_alarmed,1,[itm_hanbo,itm_kataginu_j]),
          (9,mtef_visitor_source|mtef_team_2,af_override_all,aif_start_alarmed,1,[itm_hanbo,itm_kataginu_j]),
          (10,mtef_visitor_source|mtef_team_2,af_override_all,aif_start_alarmed,1,[itm_hanbo,itm_kataginu_j]),
          (11,mtef_visitor_source|mtef_team_2,af_override_all,aif_start_alarmed,1,[itm_hanbo,itm_kataginu_j]),
          (12,mtef_visitor_source|mtef_team_2,af_override_all,aif_start_alarmed,1,[itm_practice_naginata,itm_kataginu_j]),
          (13,mtef_visitor_source|mtef_team_2,af_override_all,aif_start_alarmed,1,[itm_practice_naginata,itm_kataginu_j]),
          (14,mtef_visitor_source|mtef_team_2,af_override_all,aif_start_alarmed,1,[itm_practice_naginata,itm_kataginu_j]),
          (15,mtef_visitor_source|mtef_team_2,af_override_all,aif_start_alarmed,1,[itm_practice_naginata,itm_kataginu_j]),
          (16,mtef_visitor_source|mtef_team_2,af_override_all,aif_start_alarmed,1,[itm_light_yumi,itm_ya,itm_kataginu_j]),
          (17,mtef_visitor_source|mtef_team_2,af_override_all,aif_start_alarmed,1,[itm_light_yumi,itm_ya,itm_kataginu_j]),
          (18,mtef_visitor_source|mtef_team_2,af_override_all,aif_start_alarmed,1,[itm_light_yumi,itm_ya,itm_kataginu_j]),
          (19,mtef_visitor_source|mtef_team_2,af_override_all,aif_start_alarmed,1,[itm_light_yumi,itm_ya,itm_kataginu_j]),
          (20,mtef_visitor_source|mtef_team_2,af_override_all,aif_start_alarmed,1,[itm_kodachi,itm_shaken_shuriken,itm_kataginu_j]),
          (21,mtef_visitor_source|mtef_team_2,af_override_all,aif_start_alarmed,1,[itm_kodachi,itm_shaken_shuriken,itm_kataginu_j]),
          (22,mtef_visitor_source|mtef_team_2,af_override_all,aif_start_alarmed,1,[itm_kodachi,itm_shaken_shuriken,itm_kataginu_j]),
          (23,mtef_visitor_source|mtef_team_2,af_override_all,aif_start_alarmed,1,[itm_kodachi,itm_shaken_shuriken,itm_kataginu_j]),
          ],
      [
          (1, 3, ti_once, [(main_hero_fallen,0),
                           (assign,"$waiting_for_training_fight_result",0)],
           [(set_jump_entry,1),(jump_to_scene,"$dojo_scene")]),
          (ti_tab_pressed, 0, 0, [],
         [
           (try_begin),
             (eq, "$training_fight_won", 1),
             (finish_mission,0),
           (else_try),
             (call_script, "script_cf_check_enemies_nearby"),
             (question_box,"str_do_you_want_to_retreat",'str_yes','str_no'),
           (else_try),
             (display_message,"str_can_not_retreat"),
           (try_end),
           ]),
          (1, 3, ti_once, [(store_mission_timer_a,reg1),
                           (ge,reg1,1),
                           (num_active_teams_le,1),
                           (neg|main_hero_fallen,0),
                           (assign,"$waiting_for_training_fight_result",0),
                           (assign,"$training_fight_won",1)],
           [(set_jump_entry,1),(jump_to_scene,"$dojo_scene")]),
          ],
      ),
    (
        "entertain_visiting_kuge",mtf_no_blood,speak,
        "You are entertaining a visiting kuge.",
        [
            (0,mtef_scene_source|mtef_team_1,af_override_all,0,1,[itm_hou,itm_eboshi,itm_bokken]),
            (1,mtef_scene_source|mtef_team_1,af_override_all,0,1,[itm_hou,itm_eboshi,itm_bokken]),
            (2,mtef_visitor_source|mtef_team_1,af_override_all,0,1,[itm_hou,itm_eboshi,itm_bokken]),
            (3,mtef_visitor_source|mtef_team_1,af_override_all,0,1,[itm_hou,itm_eboshi,itm_bokken]),
            (4,mtef_visitor_source|mtef_team_1,af_override_all,0,1,[itm_hou,itm_eboshi,itm_bokken]),
            (5,mtef_visitor_source|mtef_team_1,af_override_all,0,1,[itm_hou,itm_eboshi,itm_bokken]),
            (6,mtef_visitor_source|mtef_team_1,af_override_all,0,1,[itm_hou,itm_eboshi,itm_bokken]),
            (7,mtef_visitor_source|mtef_team_1,af_override_all,0,1,[itm_hou,itm_eboshi,itm_bokken]),
            (8,mtef_visitor_source|mtef_team_1,af_override_all,0,1,[itm_hou,itm_eboshi,itm_bokken]),
        ],
        [
            (1, 3, ti_once, [(eq,"$visiting_kuge_conversation",2),
                                (assign,"$visiting_kuge_complete",1),
                                (assign,"$visiting_kuge_happy",0)],
                [
                    (jump_to_scene,"$return_to_shugo_scene")]),
            (1, 3, ti_once, [(eq,"$visiting_kuge_conversation",1),
                                (assign,"$visiting_kuge_complete",1),
                                (assign,"$visiting_kuge_happy",1)],
                [
                    (jump_to_scene,"$return_to_shugo_scene")]),
                    ]
        ),
    (
        "single_combat",mtf_battle_mode,-1,
        "You have been engaged in single combat.",
        [
            (1,mtef_defenders,af_override_horse,aif_start_alarmed,8,[]),
            (0,mtef_defenders,af_override_horse,aif_start_alarmed,0,[]),
            (4,mtef_attackers,0,aif_start_alarmed,8,[]),
            (4,mtef_attackers,0,aif_start_alarmed,0,[])
            ],
        [
            (ti_tab_pressed, 0, 0, [],
         [
           (try_begin),
             (eq, "$battle_won", 1),
             (finish_mission,0),
           (else_try),
             (call_script, "script_cf_check_enemies_nearby"),
             (question_box,"str_do_you_want_to_retreat",'str_yes','str_no'),
           (else_try),
             (display_message,"str_can_not_retreat"),
           (try_end),
           ]),
        (ti_question_answered, 0, 0, [],
         [(store_trigger_param_1,":answer"),(eq,":answer",0),(finish_mission,0),]),
        (ti_inventory_key_pressed, 0, 0, [(display_message,"str_use_baggage_for_inventory")], []),
            (1, 60, ti_once,
             [
                 (store_mission_timer_a,reg1),
                 (ge,reg1,10),
                 (all_enemies_defeated,2),
                 (neg|main_hero_fallen,0),
                 (set_mission_result,1),
                 (display_message,"str_msg_battle_won"),
                 (assign,"$battle_won",1)],
             [(finish_mission,1)]),
            (10, 0, 0, [], [(eq,"$battle_won",1),(display_message,"str_msg_battle_won")]),
            (1, 4, ti_once, [(main_hero_fallen,0)],
                    [(set_mission_result,-1),(finish_mission,1)]),
					
	(ti_before_mission_start,0,0.0,[],
	[
		(assign,"$player_mission_kills",0),
		]
	),

(ti_once, 0, 0, [],
	[
		(get_player_agent_no,':player_agent_no'),
		(store_agent_hit_points,'$player_mission_health',':player_agent_no'),
		]
	),

# weapons and armor durability code
(0.2, 0, 0, [],
	[
		(get_player_agent_kill_count,":player_kills",0),
		(assign, ":past_kills", "$player_mission_kills"),  #player_mission_kills is set to 0 whenever a mission starts.
		(assign, "$player_mission_kills", ":player_kills"),
		(get_player_agent_no,":player_agent_no"),
		(try_begin),
			(gt, ":player_kills", ":past_kills"),
			(agent_get_wielded_item,":wielded_item",":player_agent_no",0),
			(item_get_slot,':weapon_type',':wielded_item',slot_item_type),
			(try_begin),
				(eq,':weapon_type',sit_metal_weapon),
				(store_random_in_range,':weapon_damage',1,6),
				(val_add,':weapon_damage',3),
				(call_script,'script_modify_item_durability',':wielded_item',':weapon_damage'),
			(else_try),
				(eq,':weapon_type',sit_wooden_weapon),
				(store_random_in_range,':weapon_damage',0,5),
				(val_add,':weapon_damage',1),
				(call_script,'script_modify_item_durability',':wielded_item',':weapon_damage'),
			(else_try),
				(eq,':weapon_type',sit_metal_weapon),
				(store_random_in_range,':weapon_damage',1,7),
				(val_add,':weapon_damage',3),
				(call_script,'script_modify_item_durability',':wielded_item',':weapon_damage'),
			(else_try),
				(eq,':weapon_type',sit_ranged_weapon),
				(store_random_in_range,':weapon_damage',0,4),
				(val_add,':weapon_damage',2),
				(call_script,'script_modify_item_durability',':wielded_item',':weapon_damage'),
			(try_end),
		(try_end),
		(store_agent_hit_points,':current_health',':player_agent_no'),
		(try_begin),
			(lt,':current_health','$player_mission_health'),
			(assign,'$player_mission_health',':current_health'),
			# armor
			(try_begin),
				(troop_get_inventory_slot,':player_armor','trp_player',ek_body),
				(gt,':player_armor',0),
				(store_random_in_range,':armor_damage',0,6),
				(val_add,':armor_damage',3),
				(call_script,'script_modify_item_durability',':player_armor',':weapon_damage'),
			(try_end),
			# helmet
			(try_begin),
				(troop_get_inventory_slot,':player_helmet','trp_player',ek_head),
				(gt,':player_helmet',0),
				(store_random_in_range,':armor_damage',0,4),
				(val_add,':armor_damage',2),
				(call_script,'script_modify_item_durability',':player_helmet',':weapon_damage'),
			(try_end),
			# boots
			(try_begin),
				(troop_get_inventory_slot,':player_boots','trp_player',ek_foot),
				(gt,':player_boots',0),
				(store_random_in_range,':armor_damage',0,6),
				(val_add,':armor_damage',1),
				(call_script,'script_modify_item_durability',':player_boots',':weapon_damage'),
			(try_end),
			# gloves
			(try_begin),
				(troop_get_inventory_slot,':player_gloves','trp_player',ek_gloves),
				(gt,':player_gloves',0),
				(store_random_in_range,':armor_damage',0,3),
				(call_script,'script_modify_item_durability',':player_gloves',':weapon_damage'),
			(try_end),
		(try_end),
		]
	),				
	]
        ),
  (
      "duel",mtf_battle_mode,-1,
      "You have been engaged in single combat.",
      [
		(0,mtef_scene_source|mtef_leader_only,af_override_horse|af_override_armor,0,1,[itm_kataginu_j,itm_waraji_a]),
        (1,mtef_scene_source|mtef_leader_only,af_override_horse|af_override_armor,0,1,[itm_kataginu_j,itm_waraji_a]),
        (2,mtef_scene_source|mtef_leader_only,af_override_horse|af_override_armor,0,1,[itm_kataginu_j,itm_waraji_a]),
        (3,mtef_scene_source|mtef_leader_only,af_override_horse|af_override_armor,0,1,[itm_kataginu_j,itm_waraji_a]),
        (4,mtef_scene_source|mtef_leader_only,af_override_horse|af_override_armor,0,1,[itm_kataginu_j,itm_waraji_a]),
        (5,mtef_visitor_source,af_override_horse|af_override_weapons,aif_start_alarmed,1,[itm_tachi]),
        (6,mtef_visitor_source,af_override_horse|af_override_weapons,aif_start_alarmed,1,[itm_naginata]),
        (7,mtef_visitor_source,af_override_horse|af_override_weapons,aif_start_alarmed,1,[itm_yumi,itm_ya,itm_uchi_gatana]),
        (8,mtef_visitor_source,af_override_horse|af_override_weapons,aif_start_alarmed,1,[itm_bo]),
        ],
      [
          (ti_tab_pressed, 0, 0, [],
         [
           (try_begin),
             (eq, "$battle_won", 1),
             (finish_mission,0),
           (else_try),
             (call_script, "script_cf_check_enemies_nearby"),
             (question_box,"str_do_you_want_to_retreat",'str_yes','str_no'),
           (else_try),
             (display_message,"str_can_not_retreat"),
           (try_end),
           ]),
        (ti_question_answered, 0, 0, [],
         [(store_trigger_param_1,":answer"),(eq,":answer",0),(finish_mission,0),]),
        (ti_inventory_key_pressed, 0, 0, [(display_message,"str_use_baggage_for_inventory")], []),
          (1, 60, ti_once, [(store_mission_timer_a,reg1),
                            (ge,reg1,10),
                            (all_enemies_defeated,2),
                            (neg|main_hero_fallen,0),
                            (set_mission_result,1),
                            (display_message,"str_msg_battle_won"),
                            (assign,"$battle_won",1)],
           [(finish_mission,1)]),
          (10, 0, 0, [], [(eq,"$battle_won",1),(display_message,"str_msg_battle_won")]),
          (1, 4, ti_once, [(main_hero_fallen,0)],
           [(assign,'$g_battle_result',-1),(set_mission_result,-1),(finish_mission,1)]),
		  (ti_before_mission_start,0,0.0,[],[(assign,"$player_mission_kills",0),]),

# weapons and armor durability code
(0.2, 0, 0, [],
	[
		(get_player_agent_kill_count,":player_kills",0),
		(assign, ":past_kills", "$player_mission_kills"),  #player_mission_kills is set to 0 whenever a mission starts.
		(assign, "$player_mission_kills", ":player_kills"),
		(get_player_agent_no,":player_agent_no"),
		(try_begin),
			(gt, ":player_kills", ":past_kills"),
			(agent_get_wielded_item,":wielded_item",":player_agent_no",0),
			(item_get_slot,':weapon_type',':wielded_item',slot_item_type),
			(try_begin),
				(eq,':weapon_type',sit_metal_weapon),
				(store_random_in_range,':weapon_damage',1,6),
				(val_add,':weapon_damage',3),
				(call_script,'script_modify_item_durability',':wielded_item',':weapon_damage'),
			(else_try),
				(eq,':weapon_type',sit_wooden_weapon),
				(store_random_in_range,':weapon_damage',0,5),
				(val_add,':weapon_damage',1),
				(call_script,'script_modify_item_durability',':wielded_item',':weapon_damage'),
			(else_try),
				(eq,':weapon_type',sit_metal_weapon),
				(store_random_in_range,':weapon_damage',1,7),
				(val_add,':weapon_damage',3),
				(call_script,'script_modify_item_durability',':wielded_item',':weapon_damage'),
			(else_try),
				(eq,':weapon_type',sit_ranged_weapon),
				(store_random_in_range,':weapon_damage',0,4),
				(val_add,':weapon_damage',2),
				(call_script,'script_modify_item_durability',':wielded_item',':weapon_damage'),
			(try_end),
		(try_end),
		(store_agent_hit_points,':current_health',':player_agent_no'),
		(try_begin),
			(lt,':current_health','$player_mission_health'),
			(assign,'$player_mission_health',':current_health'),
			# armor
			(try_begin),
				(troop_get_inventory_slot,':player_armor','trp_player',ek_body),
				(gt,':player_armor',0),
				(store_random_in_range,':armor_damage',0,6),
				(val_add,':armor_damage',3),
				(call_script,'script_modify_item_durability',':player_armor',':weapon_damage'),
			(try_end),
			# helmet
			(try_begin),
				(troop_get_inventory_slot,':player_helmet','trp_player',ek_head),
				(gt,':player_helmet',0),
				(store_random_in_range,':armor_damage',0,4),
				(val_add,':armor_damage',2),
				(call_script,'script_modify_item_durability',':player_helmet',':weapon_damage'),
			(try_end),
			# boots
			(try_begin),
				(troop_get_inventory_slot,':player_boots','trp_player',ek_foot),
				(gt,':player_boots',0),
				(store_random_in_range,':armor_damage',0,6),
				(val_add,':armor_damage',1),
				(call_script,'script_modify_item_durability',':player_boots',':weapon_damage'),
			(try_end),
			# gloves
			(try_begin),
				(troop_get_inventory_slot,':player_gloves','trp_player',ek_gloves),
				(gt,':player_gloves',0),
				(store_random_in_range,':armor_damage',0,3),
				(call_script,'script_modify_item_durability',':player_gloves',':weapon_damage'),
			(try_end),
		(try_end),
		]
	),
          ]
      ),
# infiltratino mission templates
('infiltration',0,-1,'You try to sneak in...',
	# spawn records
	[
		# player team record
		(1,mtef_scene_source|mtef_leader_only|mtef_team_1,af_override_horse,0,1,[]),
		(2,mtef_defenders|mtef_team_1,af_override_horse,aif_start_alarmed,9,[]),
		# enemy records
		(3,mtef_visitor_source|mtef_team_2,af_override_horse,aif_start_alarmed,4,[]), # left side of shinden
		(4,mtef_visitor_source|mtef_team_2,af_override_horse,aif_start_alarmed,4,[]), # right side of shinden
		(5,mtef_visitor_source|mtef_team_2,af_override_horse,aif_start_alarmed,4,[]), # front entry of shinden
		(6,mtef_visitor_source|mtef_team_2,af_override_horse,aif_start_alarmed,1,[]), # archer positions on shinden porch
		(7,mtef_visitor_source|mtef_team_2,af_override_horse,aif_start_alarmed,1,[]),
		(8,mtef_visitor_source|mtef_team_2,af_override_horse,aif_start_alarmed,1,[]),
		(9,mtef_visitor_source|mtef_team_2,af_override_horse,aif_start_alarmed,1,[]),
		
		],
	# triggers
	[
		(1.0, 0, ti_once,[(le,'$g_infiltration_flag',0),],[(jump_to_menu,'mnu_infiltration_start'),]),
		(3.0, 0, ti_once,
			[
				(ge,'$g_infiltration_flag',1),
				],
			[
				(try_begin),
					(eq,'$g_infiltration_flag',1), # be sure to reset to 0 after mission complete
					(display_message,'str_infiltration_success'),
					(set_jump_mission,'mt_infiltration'),
					(set_jump_entry,1),
					(jump_to_scene,'scn_shinden_main_hall'),
					(change_screen_mission),
				(else_try),
					(display_message,'str_infiltration_failed'),
					(set_jump_mission,'mt_infiltration_fight'),
					(set_jump_entry,1),
					(jump_to_scene,'scn_shinden_fight'),
					(change_screen_mission),
				(try_end),
				(assign,'$g_infiltration_flag',-1),
				],
			),
		],
	),  
('shinden',0,-1,'You try to sneak in...',
	# spawn records
	[
		# player team record
		(1,mtef_scene_source|mtef_leader_only|mtef_team_1,af_override_horse,0,1,[]),
		(2,mtef_scene_source|mtef_leader_only|mtef_team_1,af_override_horse,0,1,[]),
		(3,mtef_scene_source|mtef_leader_only|mtef_team_1,af_override_horse,0,1,[]),
		(4,mtef_scene_source|mtef_leader_only|mtef_team_1,af_override_horse,0,1,[]),
		(5,mtef_scene_source|mtef_leader_only|mtef_team_1,af_override_horse,0,1,[]),
		(6,mtef_visitor_source,af_override_horse,0,1,[]),
		(7,mtef_visitor_source,af_override_horse,0,1,[]),
		(8,mtef_visitor_source,af_override_horse,0,1,[]),
		(9,mtef_visitor_source,af_override_horse,0,1,[]),
		(10,mtef_visitor_source,af_override_horse,0,1,[]),
		(11,mtef_visitor_source,af_override_horse,0,1,[]),
		(12,mtef_visitor_source,af_override_horse,0,1,[]),
		# enemy records
		],
	# triggers
	[
		(ti_inventory_key_pressed, 0, 0, [(set_trigger_result,1)], []),
		(ti_tab_pressed, 0, 0, [(set_trigger_result,1)], []),
		],
	),  
('infiltration_fight',mtf_battle_mode,-1,'You have successfully accessed the interior...',
	[
		# player team record
		(1,mtef_scene_source|mtef_leader_only|mtef_team_1,af_override_horse,0,1,[]),
		(2,mtef_defenders|mtef_team_1,af_override_horse,aif_start_alarmed,9,[]),
		# enemy records
		(3,mtef_visitor_source|mtef_team_2,af_override_horse,aif_start_alarmed,4,[]), # left side of shinden
		(4,mtef_visitor_source|mtef_team_2,af_override_horse,aif_start_alarmed,4,[]), # right side of shinden
		(5,mtef_visitor_source|mtef_team_2,af_override_horse,aif_start_alarmed,4,[]), # front entry of shinden
		(6,mtef_visitor_source|mtef_team_2,af_override_horse,aif_start_alarmed,1,[]), # archer positions on shinden porch
		(7,mtef_visitor_source|mtef_team_2,af_override_horse,aif_start_alarmed,1,[]),
		(8,mtef_visitor_source|mtef_team_2,af_override_horse,aif_start_alarmed,1,[]),
		(9,mtef_visitor_source|mtef_team_2,af_override_horse,aif_start_alarmed,1,[]),],
	[
		],
	),
]
debug = """  (
    "custom_battle",mtf_battle_mode,-1,
    "You lead your men to battle.",
    [
      (0,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (1,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (2,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (3,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (4,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (5,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (6,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (7,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),

      (8,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (9,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (10,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (11,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (12,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (13,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (14,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (15,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),

      (16,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (17,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (18,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (19,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (20,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (21,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (22,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (23,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),

      (24,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (25,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (26,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (27,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (28,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (29,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (30,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (31,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
     ],
    [
                
      (ti_tab_pressed, 0, 0, [],
       [(question_box,"str_give_up_fight")]),

      (ti_question_answered, 0, 0, [],
       [(store_trigger_param_1,":answer"),
        (eq,":answer",0),
        (assign, "$g_battle_result", -1),
        (assign, "$g_custom_battle_team1_death_count", 0),
        (assign, "$g_custom_battle_team2_death_count", 0),
        (try_for_agents, ":cur_agent"),
          (agent_is_human, ":cur_agent"),
          (neg|agent_is_alive, ":cur_agent"),
          (agent_get_team, ":cur_team", ":cur_agent"),
          (try_begin),
            (eq, ":cur_team", 0),
            (val_add, "$g_custom_battle_team1_death_count", 1),
          (else_try),
            (val_add, "$g_custom_battle_team2_death_count", 1),
          (try_end),
        (try_end),
        (finish_mission),]),
  
      (ti_inventory_key_pressed, 0, 0, [(display_message,"str_cant_use_inventory_now")], []),

      (1, 4, ti_once, [(this_or_next|main_hero_fallen),(num_active_teams_le,1)],
       [
           (assign,"$g_battle_result",0),
           (try_begin),
             (neg|main_hero_fallen),
             (assign,"$g_battle_result",1),
           (else_try),
             (assign,"$g_battle_result",-1),
           (try_end),
           (assign, "$g_custom_battle_team1_death_count", 0),
           (assign, "$g_custom_battle_team2_death_count", 0),
           (try_for_agents, ":cur_agent"),
             (agent_is_human, ":cur_agent"),
             (neg|agent_is_alive, ":cur_agent"),
             (agent_get_team, ":cur_team", ":cur_agent"),
             (try_begin),
               (eq, ":cur_team", 0),
               (val_add, "$g_custom_battle_team1_death_count", 1),
             (else_try),
               (val_add, "$g_custom_battle_team2_death_count", 1),
             (try_end),
           (try_end),
           (finish_mission),
           ]),

]

##      #AI Tiggers
##      (0, 0, ti_once, [
##          (store_mission_timer_a,reg1),(ge,reg1,4),
##          (call_script, "script_select_battle_tactic"),
##          (call_script, "script_battle_tactic_init"),
##          ], []),
##      (1, 0, 0, [
##          (store_mission_timer_a,reg1),(ge,reg1,4),
##          (call_script, "script_battle_tactic_apply"),
##          ], []),
      
    ],
  ),

  (
    "custom_battle_siege",mtf_battle_mode,-1,
    "You lead your men to battle.",
    [
      (0,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (1,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (2,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (3,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (4,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (5,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (6,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (7,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),

      (8,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (9,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (10,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (11,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (12,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (13,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (14,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),
      (15,mtef_visitor_source|mtef_team_0,0,aif_start_alarmed,1,[]),

      (16,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (17,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (18,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (19,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (20,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (21,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (22,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (23,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),

      (24,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (25,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (26,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (27,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (28,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (29,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (30,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
      (31,mtef_visitor_source|mtef_team_1,0,aif_start_alarmed,1,[]),
     ],
    [
      (ti_before_mission_start, 0, 0, [], [(call_script, "script_change_banners")]),
                
      (ti_tab_pressed, 0, 0, [],
       [(question_box,"str_give_up_fight")]),

      (ti_question_answered, 0, 0, [],
       [(store_trigger_param_1,":answer"),
        (eq,":answer",0),
        (assign, "$g_battle_result", -1),
        (assign, "$g_custom_battle_team1_death_count", 0),
        (assign, "$g_custom_battle_team2_death_count", 0),
        (try_for_agents, ":cur_agent"),
          (agent_is_human, ":cur_agent"),
          (neg|agent_is_alive, ":cur_agent"),
          (agent_get_team, ":cur_team", ":cur_agent"),
          (try_begin),
            (eq, ":cur_team", 0),
            (val_add, "$g_custom_battle_team1_death_count", 1),
          (else_try),
            (val_add, "$g_custom_battle_team2_death_count", 1),
          (try_end),
        (try_end),
        (finish_mission),]),
  
      (ti_inventory_key_pressed, 0, 0, [(display_message,"str_cant_use_inventory_now")], []),

      (1, 4, ti_once, [(this_or_next|main_hero_fallen),(num_active_teams_le,1)],
       [
           (assign,"$g_battle_result",0),
           (try_begin),
             (neg|main_hero_fallen),
             (assign,"$g_battle_result",1),
           (else_try),
             (assign,"$g_battle_result",-1),
           (try_end),
           (assign, "$g_custom_battle_team1_death_count", 0),
           (assign, "$g_custom_battle_team2_death_count", 0),
           (try_for_agents, ":cur_agent"),
             (agent_is_human, ":cur_agent"),
             (neg|agent_is_alive, ":cur_agent"),
             (agent_get_team, ":cur_team", ":cur_agent"),
             (try_begin),
               (eq, ":cur_team", 0),
               (val_add, "$g_custom_battle_team1_death_count", 1),
             (else_try),
               (val_add, "$g_custom_battle_team2_death_count", 1),
             (try_end),
           (try_end),
           (finish_mission),
           ]),

#refill ammo of defenders every two minutes.
      (120, 0, 0, [], [(get_player_agent_no, ":player_agent"),
                      (try_for_agents,":cur_agent"),
                        (neq, ":cur_agent", ":player_agent"),
                        (agent_is_defender, ":cur_agent"),
                        (agent_is_alive, ":cur_agent"),
                        (agent_is_human, ":cur_agent"),
                        (agent_refill_ammo, ":cur_agent"),
                      (try_end)]),

      #Init AI and Belfry
      (0, 0, ti_once, [
          (call_script, "script_siege_init_ai_and_belfry"),
          ], []),


      #Moving belfry
      (0, 0, ti_once, [(call_script, "script_cf_siege_move_belfry")], []),

      #Rotating belfry platform at the end
      (0, 4, ti_once, [(call_script, "script_cf_siege_rotate_belfry_platform"),
                       ], [(assign, "$belfry_positioned", 3)]),

      #Assigning men to the belfry
      (0, 0, ti_once, [(call_script, "script_cf_siege_assign_men_to_belfry")], []),
      #AI Tiggers
      (0, 0, ti_once, [
          (set_show_messages,0),
          (assign, "$defender_team", 0),
          (team_give_order, "$defender_team", grc_infantry, mordr_stand_closer),
          (team_give_order, "$defender_team", grc_infantry, mordr_stand_closer),
          (team_give_order, "$defender_team", grc_archers, mordr_stand_ground),
          (entry_point_get_position,pos8,10),
          (team_give_order, "$defender_team", grc_infantry, mordr_hold),
          (team_set_order_position, "$defender_team", grc_everyone, pos8),
          (set_show_messages,1),
          ], []),
    ],
  ),

  (
    "custom_battle_5",mtf_battle_mode,-1,
    "You lead your men to battle.",
    [
      (0,mtef_visitor_source|mtef_team_0,af_override_horse,aif_start_alarmed,1,[]),
      (1,mtef_visitor_source|mtef_team_0,af_override_horse,aif_start_alarmed,1,[]),
      (2,mtef_visitor_source|mtef_team_0,af_override_horse,aif_start_alarmed,1,[]),
      (3,mtef_visitor_source|mtef_team_0,af_override_horse,aif_start_alarmed,1,[]),
      (4,mtef_visitor_source|mtef_team_0,af_override_horse,aif_start_alarmed,1,[]),
      (5,mtef_visitor_source|mtef_team_0,af_override_horse,aif_start_alarmed,1,[]),
      (6,mtef_visitor_source|mtef_team_0,af_override_horse,aif_start_alarmed,1,[]),
      (7,mtef_visitor_source|mtef_team_0,af_override_horse,aif_start_alarmed,1,[]),

      (8,mtef_visitor_source|mtef_team_1,af_override_horse,aif_start_alarmed,1,[]),
      (9,mtef_visitor_source|mtef_team_1,af_override_horse,aif_start_alarmed,1,[]),
      (10,mtef_visitor_source|mtef_team_1,af_override_horse,aif_start_alarmed,1,[]),
      (11,mtef_visitor_source|mtef_team_1,af_override_horse,aif_start_alarmed,1,[]),
      (12,mtef_visitor_source|mtef_team_1,af_override_horse,aif_start_alarmed,1,[]),
      (13,mtef_visitor_source|mtef_team_1,af_override_horse,aif_start_alarmed,1,[]),
      (14,mtef_visitor_source|mtef_team_1,af_override_horse,aif_start_alarmed,1,[]),
      (15,mtef_visitor_source|mtef_team_1,af_override_horse,aif_start_alarmed,1,[]),
     ],
    [
                
      (ti_tab_pressed, 0, 0, [],
       [(question_box,"str_give_up_fight")]),

      (ti_question_answered, 0, 0, [],
       [(store_trigger_param_1,":answer"),
        (eq,":answer",0),
        (assign, "$g_battle_result", -1),
        (assign, "$g_custom_battle_team1_death_count", 0),
        (assign, "$g_custom_battle_team2_death_count", 0),
        (try_for_agents, ":cur_agent"),
          (agent_is_human, ":cur_agent"),
          (neg|agent_is_alive, ":cur_agent"),
          (agent_get_team, ":cur_team", ":cur_agent"),
          (try_begin),
            (eq, ":cur_team", 0),
            (val_add, "$g_custom_battle_team1_death_count", 1),
          (else_try),
            (val_add, "$g_custom_battle_team2_death_count", 1),
          (try_end),
        (try_end),
        (finish_mission),]),
  
      (ti_inventory_key_pressed, 0, 0, [(display_message,"str_cant_use_inventory_now")], []),

      (1, 4, ti_once, [(this_or_next|main_hero_fallen),(num_active_teams_le,1)],
       [
           (assign,"$g_battle_result",0),
           (try_begin),
             (neg|main_hero_fallen),
             (assign,"$g_battle_result",1),
           (else_try),
             (assign,"$g_battle_result",-1),
           (try_end),
           (assign, "$g_custom_battle_team1_death_count", 0),
           (assign, "$g_custom_battle_team2_death_count", 0),
           (try_for_agents, ":cur_agent"),
             (agent_is_human, ":cur_agent"),
             (neg|agent_is_alive, ":cur_agent"),
             (agent_get_team, ":cur_team", ":cur_agent"),
             (try_begin),
               (eq, ":cur_team", 0),
               (val_add, "$g_custom_battle_team1_death_count", 1),
             (else_try),
               (val_add, "$g_custom_battle_team2_death_count", 1),
             (try_end),
           (try_end),
           (finish_mission),
           ]),

      #AI Tiggers
      (0, 0, ti_once, [
          (assign, "$ai_team", 1),
          (team_give_order, "$ai_team", grc_infantry, mordr_stand_closer),
          (team_give_order, "$ai_team", grc_infantry, mordr_stand_closer),
          (team_give_order, "$ai_team", grc_archers, mordr_stand_ground),
          (entry_point_get_position,pos8,8),
          (team_give_order, "$ai_team", grc_infantry, mordr_hold),
          (team_set_order_position, "$ai_team", grc_everyone, pos8),
          ], []),
      
      (5, 0, 0, [], [ #Make sure attackers do not stall on the ladders...
          (assign, "$attacker_team", 0),
          (try_for_agents, ":agent_no"),
            (agent_is_human, ":agent_no"),
            (agent_is_alive, ":agent_no"),
            (agent_get_team, ":agent_team", ":agent_no"),
            (eq, "$attacker_team", ":agent_team"),
            (agent_ai_set_always_attack_in_melee, ":agent_no", 1),
          (try_end),
          ]),
#refill ammo of defenders every two minutes.
      (120, 0, 0, [], [
                      (assign, "$defender_team", 1),
                      (try_for_agents,":cur_agent"),
                        (agent_get_team, ":agent_team", ":cur_agent"),
                        (eq, "$defender_team", ":agent_team"),
                        (agent_is_alive, ":cur_agent"),
                        (agent_is_human, ":cur_agent"),
                        (agent_refill_ammo, ":cur_agent"),
                      (try_end)]),
      
    ],
  ),"""